@oscarpalmer/jhunal 0.17.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.d.mts +28 -15
- package/dist/constants.mjs +31 -14
- package/dist/helpers.d.mts +8 -1
- package/dist/helpers.mjs +68 -3
- package/dist/index.d.mts +304 -240
- package/dist/index.mjs +201 -51
- package/dist/models/infer.model.d.mts +66 -0
- package/dist/models/infer.model.mjs +1 -0
- package/dist/models/misc.model.d.mts +153 -0
- package/dist/models/misc.model.mjs +1 -0
- package/dist/models/schema.plain.model.d.mts +92 -0
- package/dist/models/schema.plain.model.mjs +1 -0
- package/dist/models/schema.typed.model.d.mts +96 -0
- package/dist/models/schema.typed.model.mjs +1 -0
- package/dist/models/transform.model.d.mts +59 -0
- package/dist/models/transform.model.mjs +1 -0
- package/dist/models/validation.model.d.mts +82 -0
- package/dist/models/validation.model.mjs +21 -0
- package/dist/schematic.d.mts +34 -1
- package/dist/schematic.mjs +11 -12
- package/dist/validation/property.validation.d.mts +1 -1
- package/dist/validation/property.validation.mjs +21 -17
- package/dist/validation/value.validation.d.mts +2 -2
- package/dist/validation/value.validation.mjs +73 -12
- package/package.json +2 -2
- package/src/constants.ts +84 -19
- package/src/helpers.ts +162 -4
- package/src/index.ts +3 -1
- package/src/models/infer.model.ts +105 -0
- package/src/models/misc.model.ts +212 -0
- package/src/models/schema.plain.model.ts +110 -0
- package/src/models/schema.typed.model.ts +109 -0
- package/src/models/transform.model.ts +85 -0
- package/src/models/validation.model.ts +124 -0
- package/src/schematic.ts +55 -10
- package/src/validation/property.validation.ts +41 -36
- package/src/validation/value.validation.ts +133 -20
- package/dist/models.d.mts +0 -484
- package/dist/models.mjs +0 -13
- package/src/models.ts +0 -665
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import type {SchemaProperty} from './schema.plain.model';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Removes duplicate types from a tuple, preserving first occurrence order
|
|
5
|
+
*
|
|
6
|
+
* @template Value - Tuple to deduplicate
|
|
7
|
+
* @template Seen - Accumulator for already-seen types _(internal)_
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* // DeduplicateTuple<['string', 'number', 'string']>
|
|
12
|
+
* // => ['string', 'number']
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export type DeduplicateTuple<Value extends unknown[], Seen extends unknown[] = []> = Value extends [
|
|
16
|
+
infer Head,
|
|
17
|
+
...infer Tail,
|
|
18
|
+
]
|
|
19
|
+
? Head extends Seen[number]
|
|
20
|
+
? DeduplicateTuple<Tail, Seen>
|
|
21
|
+
: DeduplicateTuple<Tail, [...Seen, Head]>
|
|
22
|
+
: Seen;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Recursively extracts {@link ValueName} strings from a type, unwrapping arrays and readonly arrays
|
|
26
|
+
*
|
|
27
|
+
* @template Value - Type to extract value names from
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* // ExtractValueNames<'string'> => 'string'
|
|
32
|
+
* // ExtractValueNames<['string', 'number']> => 'string' | 'number'
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export type ExtractValueNames<Value> = Value extends ValueName
|
|
36
|
+
? Value
|
|
37
|
+
: Value extends (infer Item)[]
|
|
38
|
+
? ExtractValueNames<Item>
|
|
39
|
+
: Value extends readonly (infer Item)[]
|
|
40
|
+
? ExtractValueNames<Item>
|
|
41
|
+
: never;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Determines whether a schema entry is optional
|
|
45
|
+
*
|
|
46
|
+
* Returns `true` if the entry is a {@link SchemaProperty} or {@link NestedSchema} with `$required` set to `false`; otherwise returns `false`
|
|
47
|
+
*
|
|
48
|
+
* @template Value - Schema entry to check
|
|
49
|
+
*/
|
|
50
|
+
export type IsOptionalProperty<Value> = Value extends SchemaProperty
|
|
51
|
+
? Value['$required'] extends false
|
|
52
|
+
? true
|
|
53
|
+
: false
|
|
54
|
+
: false;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Extracts the last member from a union type by leveraging intersection of function return types
|
|
58
|
+
*
|
|
59
|
+
* @template Value - Union type
|
|
60
|
+
*/
|
|
61
|
+
export type LastOfUnion<Value> =
|
|
62
|
+
UnionToIntersection<Value extends unknown ? () => Value : never> extends () => infer Item
|
|
63
|
+
? Item
|
|
64
|
+
: never;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Extracts keys from an object type that are optional
|
|
68
|
+
*
|
|
69
|
+
* @template Value - Object type to inspect
|
|
70
|
+
*/
|
|
71
|
+
export type OptionalKeys<Value> = {
|
|
72
|
+
[Key in keyof Value]-?: {} extends Pick<Value, Key> ? Key : never;
|
|
73
|
+
}[keyof Value];
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Extracts keys from an object type that are required _(i.e., not optional)_
|
|
77
|
+
*
|
|
78
|
+
* @template Value - Object type to inspect
|
|
79
|
+
*/
|
|
80
|
+
export type RequiredKeys<Value> = Exclude<keyof Value, OptionalKeys<Value>>;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Generates all permutations of a tuple type
|
|
84
|
+
*
|
|
85
|
+
* Used by {@link UnwrapSingle} to allow schema types in any order for small tuples _(length ≤ 5)_
|
|
86
|
+
*
|
|
87
|
+
* @template Tuple - Tuple to permute
|
|
88
|
+
* @template Elput - Accumulator for the current permutation _(internal; name is Tuple backwards)_
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* // TuplePermutations<['string', 'number']>
|
|
93
|
+
* // => ['string', 'number'] | ['number', 'string']
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export type TuplePermutations<
|
|
97
|
+
Tuple extends unknown[],
|
|
98
|
+
Elput extends unknown[] = [],
|
|
99
|
+
> = Tuple['length'] extends 0
|
|
100
|
+
? Elput
|
|
101
|
+
: {
|
|
102
|
+
[Key in keyof Tuple]: TuplePermutations<
|
|
103
|
+
TupleRemoveAt<Tuple, Key & `${number}`>,
|
|
104
|
+
[...Elput, Tuple[Key]]
|
|
105
|
+
>;
|
|
106
|
+
}[keyof Tuple & `${number}`];
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Removes the element at a given index from a tuple
|
|
110
|
+
*
|
|
111
|
+
* Used internally by {@link TuplePermutations}
|
|
112
|
+
*
|
|
113
|
+
* @template Items - Tuple to remove from
|
|
114
|
+
* @template Item - Stringified index to remove
|
|
115
|
+
* @template Prefix - Accumulator for elements before the target _(internal)_
|
|
116
|
+
*/
|
|
117
|
+
export type TupleRemoveAt<
|
|
118
|
+
Items extends unknown[],
|
|
119
|
+
Item extends string,
|
|
120
|
+
Prefix extends unknown[] = [],
|
|
121
|
+
> = Items extends [infer Head, ...infer Tail]
|
|
122
|
+
? `${Prefix['length']}` extends Item
|
|
123
|
+
? [...Prefix, ...Tail]
|
|
124
|
+
: TupleRemoveAt<Tail, Item, [...Prefix, Head]>
|
|
125
|
+
: Prefix;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Converts a union type into an intersection
|
|
129
|
+
*
|
|
130
|
+
* Uses the contravariance of function parameter types to collapse a union into an intersection
|
|
131
|
+
*
|
|
132
|
+
* @template Value - Union type to convert
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* // UnionToIntersection<{ a: 1 } | { b: 2 }>
|
|
137
|
+
* // => { a: 1 } & { b: 2 }
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export type UnionToIntersection<Value> = (
|
|
141
|
+
Value extends unknown ? (value: Value) => void : never
|
|
142
|
+
) extends (value: infer Item) => void
|
|
143
|
+
? Item
|
|
144
|
+
: never;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Converts a union type into an ordered tuple
|
|
148
|
+
*
|
|
149
|
+
* Repeatedly extracts the {@link LastOfUnion} member and prepends it to the accumulator
|
|
150
|
+
*
|
|
151
|
+
* @template Value - Union type to convert
|
|
152
|
+
* @template Items - Accumulator for the resulting tuple _(internal)_
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* // UnionToTuple<'a' | 'b' | 'c'>
|
|
157
|
+
* // => ['a', 'b', 'c']
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
export type UnionToTuple<Value, Items extends unknown[] = []> = [Value] extends [never]
|
|
161
|
+
? Items
|
|
162
|
+
: UnionToTuple<Exclude<Value, LastOfUnion<Value>>, [LastOfUnion<Value>, ...Items]>;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Unwraps a single-element tuple to its inner type
|
|
166
|
+
*
|
|
167
|
+
* For tuples of length 2–5, returns all {@link TuplePermutations} to allow types in any order. Longer tuples are returned as-is
|
|
168
|
+
*
|
|
169
|
+
* @template Value - Tuple to potentially unwrap
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* // UnwrapSingle<['string']> => 'string'
|
|
174
|
+
* // UnwrapSingle<['string', 'number']> => ['string', 'number'] | ['number', 'string']
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
export type UnwrapSingle<Value extends unknown[]> = Value extends [infer Only]
|
|
178
|
+
? Only
|
|
179
|
+
: Value['length'] extends 1 | 2 | 3 | 4 | 5
|
|
180
|
+
? TuplePermutations<Value>
|
|
181
|
+
: Value;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Basic value types
|
|
185
|
+
*/
|
|
186
|
+
export type ValueName = keyof Values;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Maps type name strings to their TypeScript equivalents
|
|
190
|
+
*
|
|
191
|
+
* Used by the type system to resolve {@link ValueName} strings into actual types
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* // Values['string'] => string
|
|
196
|
+
* // Values['date'] => Date
|
|
197
|
+
* // Values['null'] => null
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
export type Values = {
|
|
201
|
+
array: unknown[];
|
|
202
|
+
bigint: bigint;
|
|
203
|
+
boolean: boolean;
|
|
204
|
+
date: Date;
|
|
205
|
+
function: Function;
|
|
206
|
+
null: null;
|
|
207
|
+
number: number;
|
|
208
|
+
object: object;
|
|
209
|
+
string: string;
|
|
210
|
+
symbol: symbol;
|
|
211
|
+
undefined: undefined;
|
|
212
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type {Constructor} from '@oscarpalmer/atoms/models';
|
|
2
|
+
import type {Schematic} from '../schematic';
|
|
3
|
+
import type {ExtractValueNames, ValueName, Values} from './misc.model';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A generic schema allowing {@link NestedSchema}, {@link SchemaEntry}, or arrays of {@link SchemaEntry} as values
|
|
7
|
+
*/
|
|
8
|
+
export type PlainSchema = {
|
|
9
|
+
[key: string]: PlainSchema | SchemaEntry | SchemaEntry[] | undefined;
|
|
10
|
+
} & {
|
|
11
|
+
$required?: never;
|
|
12
|
+
$type?: never;
|
|
13
|
+
$validators?: never;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A schema for validating objects
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const schema: Schema = {
|
|
22
|
+
* name: 'string',
|
|
23
|
+
* age: 'number',
|
|
24
|
+
* tags: ['string', 'number'],
|
|
25
|
+
* };
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export type Schema = SchemaIndex;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A union of all valid types for a single schema entry
|
|
32
|
+
*
|
|
33
|
+
* Can be a {@link Constructor}, nested {@link Schema}, {@link SchemaProperty}, {@link Schematic}, {@link ValueName} string, or a custom validator function
|
|
34
|
+
*/
|
|
35
|
+
export type SchemaEntry =
|
|
36
|
+
| Constructor
|
|
37
|
+
| PlainSchema
|
|
38
|
+
| SchemaProperty
|
|
39
|
+
| Schematic<unknown>
|
|
40
|
+
| ValueName
|
|
41
|
+
| ((value: unknown) => boolean);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Index signature interface backing {@link Schema}, allowing string-keyed entries of {@link NestedSchema}, {@link SchemaEntry}, or arrays of {@link SchemaEntry}
|
|
45
|
+
*/
|
|
46
|
+
export interface SchemaIndex {
|
|
47
|
+
[key: string]: PlainSchema | SchemaEntry | SchemaEntry[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A property definition with explicit type(s), an optional requirement flag, and optional validators
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const prop: SchemaProperty = {
|
|
56
|
+
* $required: false,
|
|
57
|
+
* $type: ['string', 'number'],
|
|
58
|
+
* $validators: {
|
|
59
|
+
* string: (v) => v.length > 0,
|
|
60
|
+
* number: (v) => v > 0,
|
|
61
|
+
* },
|
|
62
|
+
* };
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export type SchemaProperty = {
|
|
66
|
+
/**
|
|
67
|
+
* Whether the property is required _(defaults to `true`)_
|
|
68
|
+
*/
|
|
69
|
+
$required?: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* The type(s) the property value must match; a single {@link SchemaPropertyType} or an array
|
|
72
|
+
*/
|
|
73
|
+
$type: SchemaPropertyType | SchemaPropertyType[];
|
|
74
|
+
/**
|
|
75
|
+
* Optional validators keyed by {@link ValueName}, applied during validation
|
|
76
|
+
*/
|
|
77
|
+
$validators?: PropertyValidators<SchemaPropertyType | SchemaPropertyType[]>;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A union of valid types for a {@link SchemaProperty}'s `$type` field
|
|
82
|
+
*
|
|
83
|
+
* Can be a {@link Constructor}, {@link PlainSchema}, {@link Schematic}, {@link ValueName} string, or a custom validator function
|
|
84
|
+
*/
|
|
85
|
+
export type SchemaPropertyType =
|
|
86
|
+
| Constructor
|
|
87
|
+
| PlainSchema
|
|
88
|
+
| Schematic<unknown>
|
|
89
|
+
| ValueName
|
|
90
|
+
| ((value: unknown) => boolean);
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* A map of optional validator functions keyed by {@link ValueName}, used to add custom validation to {@link SchemaProperty} definitions
|
|
94
|
+
*
|
|
95
|
+
* Each key may hold a single validator or an array of validators that receive the typed value
|
|
96
|
+
*
|
|
97
|
+
* @template Value - `$type` value(s) to derive validator keys from
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const validators: PropertyValidators<'string'> = {
|
|
102
|
+
* string: (value) => value.length > 0,
|
|
103
|
+
* };
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export type PropertyValidators<Value> = {
|
|
107
|
+
[Key in ExtractValueNames<Value>]?:
|
|
108
|
+
| ((value: Values[Key]) => boolean)
|
|
109
|
+
| Array<(value: Values[Key]) => boolean>;
|
|
110
|
+
};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type {PlainObject, Simplify} from '@oscarpalmer/atoms/models';
|
|
2
|
+
import type {Schematic} from '../schematic';
|
|
3
|
+
import type {OptionalKeys, RequiredKeys} from './misc.model';
|
|
4
|
+
import type {PropertyValidators} from './schema.plain.model';
|
|
5
|
+
import type {ToSchemaPropertyType, ToSchemaType} from './transform.model';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A typed optional property definition generated by {@link TypedSchema} for optional keys, with `$required` set to `false` and excludes `undefined` from the type
|
|
9
|
+
*
|
|
10
|
+
* @template Value - Property's type _(including `undefined`)_
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* // For `{ name?: string }`, the `name` key produces:
|
|
15
|
+
* // TypedPropertyOptional<string | undefined>
|
|
16
|
+
* // => { $required: false; $type: 'string'; ... }
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export type TypedPropertyOptional<Value> = {
|
|
20
|
+
/**
|
|
21
|
+
* The property is not required
|
|
22
|
+
*/
|
|
23
|
+
$required: false;
|
|
24
|
+
/**
|
|
25
|
+
* The type(s) of the property
|
|
26
|
+
*/
|
|
27
|
+
$type: ToSchemaPropertyType<Exclude<Value, undefined>>;
|
|
28
|
+
/**
|
|
29
|
+
* Custom validators for the property and its types
|
|
30
|
+
*/
|
|
31
|
+
$validators?: PropertyValidators<ToSchemaPropertyType<Exclude<Value, undefined>>>;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A typed required property definition generated by {@link TypedSchema} for required keys, with `$required` defaulting to `true`
|
|
36
|
+
*
|
|
37
|
+
* @template Value - Property's type
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* // For `{ name: string }`, the `name` key produces:
|
|
42
|
+
* // TypedPropertyRequired<string>
|
|
43
|
+
* // => { $required?: true; $type: 'string'; ... }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export type TypedPropertyRequired<Value> = {
|
|
47
|
+
/**
|
|
48
|
+
* The property is required _(defaults to `true`)_
|
|
49
|
+
*/
|
|
50
|
+
$required?: true;
|
|
51
|
+
/**
|
|
52
|
+
* The type(s) of the property
|
|
53
|
+
*/
|
|
54
|
+
$type: ToSchemaPropertyType<Value>;
|
|
55
|
+
/**
|
|
56
|
+
* Custom validators for the property and its types
|
|
57
|
+
*/
|
|
58
|
+
$validators?: PropertyValidators<ToSchemaPropertyType<Value>>;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Creates a schema type constrained to match a TypeScript type
|
|
63
|
+
*
|
|
64
|
+
* Required keys map to {@link ToSchemaType} or {@link TypedPropertyRequired}; plain object values may also use {@link Schematic}. Optional keys map to {@link TypedPropertyOptional} or, for plain objects, {@link TypedSchemaOptional}
|
|
65
|
+
*
|
|
66
|
+
* @template Model - Object type to generate a schema for
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* type User = { name: string; age: number; bio?: string };
|
|
71
|
+
*
|
|
72
|
+
* const schema: TypedSchema<User> = {
|
|
73
|
+
* name: 'string',
|
|
74
|
+
* age: 'number',
|
|
75
|
+
* bio: { $required: false, $type: 'string' },
|
|
76
|
+
* };
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export type TypedSchema<Model extends PlainObject> = Simplify<
|
|
80
|
+
{
|
|
81
|
+
[Key in RequiredKeys<Model>]: Model[Key] extends PlainObject
|
|
82
|
+
? TypedSchemaRequired<Model[Key]> | Schematic<Model[Key]>
|
|
83
|
+
: ToSchemaType<Model[Key]> | TypedPropertyRequired<Model[Key]>;
|
|
84
|
+
} & {
|
|
85
|
+
[Key in OptionalKeys<Model>]: Exclude<Model[Key], undefined> extends PlainObject
|
|
86
|
+
?
|
|
87
|
+
| TypedSchemaOptional<Exclude<Model[Key], undefined>>
|
|
88
|
+
| Schematic<Exclude<Model[Key], undefined>>
|
|
89
|
+
: TypedPropertyOptional<Model[Key]>;
|
|
90
|
+
}
|
|
91
|
+
>;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* A {@link TypedSchema} variant for optional nested objects, with `$required` fixed to `false`
|
|
95
|
+
*
|
|
96
|
+
* @template Model - Nested object type
|
|
97
|
+
*/
|
|
98
|
+
type TypedSchemaOptional<Model extends PlainObject> = {
|
|
99
|
+
$required: false;
|
|
100
|
+
} & TypedSchema<Model>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* A {@link TypedSchema} variant for required nested objects, with `$required` defaulting to `true`
|
|
104
|
+
*
|
|
105
|
+
* @template Model - Nested object type
|
|
106
|
+
*/
|
|
107
|
+
type TypedSchemaRequired<Model extends PlainObject> = {
|
|
108
|
+
$required?: true;
|
|
109
|
+
} & TypedSchema<Model>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type {PlainObject} from '@oscarpalmer/atoms/models';
|
|
2
|
+
import type {Schematic} from '../schematic';
|
|
3
|
+
import type {DeduplicateTuple, UnionToTuple, UnwrapSingle, Values} from './misc.model';
|
|
4
|
+
import type {TypedSchema} from './schema.typed.model';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Maps each element of a tuple through {@link ToValueType}
|
|
8
|
+
*
|
|
9
|
+
* @template Value - Tuple of types to map
|
|
10
|
+
*/
|
|
11
|
+
export type MapToValueTypes<Value extends unknown[]> = Value extends [infer Head, ...infer Tail]
|
|
12
|
+
? [ToValueType<Head>, ...MapToValueTypes<Tail>]
|
|
13
|
+
: [];
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Maps each element of a tuple through {@link ToSchemaPropertyTypeEach}
|
|
17
|
+
*
|
|
18
|
+
* @template Value - Tuple of types to map
|
|
19
|
+
*/
|
|
20
|
+
export type MapToSchemaPropertyTypes<Value extends unknown[]> = Value extends [
|
|
21
|
+
infer Head,
|
|
22
|
+
...infer Tail,
|
|
23
|
+
]
|
|
24
|
+
? [ToSchemaPropertyTypeEach<Head>, ...MapToSchemaPropertyTypes<Tail>]
|
|
25
|
+
: [];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Converts a type into its corresponding {@link SchemaPropertyType}-representation
|
|
29
|
+
*
|
|
30
|
+
* Deduplicates and unwraps single-element tuples via {@link UnwrapSingle}
|
|
31
|
+
*
|
|
32
|
+
* @template Value - type to convert
|
|
33
|
+
*/
|
|
34
|
+
export type ToSchemaPropertyType<Value> = UnwrapSingle<
|
|
35
|
+
DeduplicateTuple<MapToSchemaPropertyTypes<UnionToTuple<Value>>>
|
|
36
|
+
>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Converts a single type to its schema property equivalent
|
|
40
|
+
*
|
|
41
|
+
* {@link NestedSchema} values have `$required` stripped, plain objects become {@link TypedSchema}, and primitives go through {@link ToValueType}
|
|
42
|
+
*
|
|
43
|
+
* @template Value - type to convert
|
|
44
|
+
*/
|
|
45
|
+
export type ToSchemaPropertyTypeEach<Value> = Value extends PlainObject
|
|
46
|
+
? TypedSchema<Value>
|
|
47
|
+
: ToValueType<Value>;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Converts a type into its corresponding {@link ValueName}-representation
|
|
51
|
+
*
|
|
52
|
+
* Deduplicates and unwraps single-element tuples via {@link UnwrapSingle}
|
|
53
|
+
*
|
|
54
|
+
* @template Value - type to convert
|
|
55
|
+
*/
|
|
56
|
+
export type ToSchemaType<Value> = UnwrapSingle<
|
|
57
|
+
DeduplicateTuple<MapToValueTypes<UnionToTuple<Value>>>
|
|
58
|
+
>;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Maps a type to its {@link ValueName} string equivalent
|
|
62
|
+
*
|
|
63
|
+
* Resolves {@link Schematic} types as-is, then performs a reverse-lookup against {@link Values} _(excluding `'object'`)_ to find a matching key. If no match is found, `object` types resolve to `'object'` or a type-guard function, and all other unrecognised types resolve to a type-guard function
|
|
64
|
+
*
|
|
65
|
+
* @template Value - type to map
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* // ToValueType<string> => 'string'
|
|
70
|
+
* // ToValueType<number[]> => 'array'
|
|
71
|
+
* // ToValueType<Date> => 'date'
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export type ToValueType<Value> =
|
|
75
|
+
Value extends Schematic<any>
|
|
76
|
+
? Value
|
|
77
|
+
: {
|
|
78
|
+
[Key in keyof Omit<Values, 'object'>]: Value extends Values[Key] ? Key : never;
|
|
79
|
+
}[keyof Omit<Values, 'object'>] extends infer Match
|
|
80
|
+
? [Match] extends [never]
|
|
81
|
+
? Value extends object
|
|
82
|
+
? 'object' | ((value: unknown) => value is Value)
|
|
83
|
+
: (value: unknown) => value is Value
|
|
84
|
+
: Match
|
|
85
|
+
: never;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type {GenericCallback} from '@oscarpalmer/atoms/models';
|
|
2
|
+
import {join} from '@oscarpalmer/atoms/string';
|
|
3
|
+
import {NAME_ERROR_SCHEMATIC, NAME_ERROR_VALIDATION} from '../constants';
|
|
4
|
+
import type {Schematic} from '../schematic';
|
|
5
|
+
import type {ValueName} from './misc.model';
|
|
6
|
+
|
|
7
|
+
// #region Reporting
|
|
8
|
+
|
|
9
|
+
export type ReportingInformation = Record<ReportingType, boolean>;
|
|
10
|
+
|
|
11
|
+
export type ReportingType = 'all' | 'first' | 'none' | 'throw';
|
|
12
|
+
|
|
13
|
+
// #endregion
|
|
14
|
+
|
|
15
|
+
// #region Schematic validation
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A custom error class for schematic validation failures
|
|
19
|
+
*/
|
|
20
|
+
export class SchematicError extends Error {
|
|
21
|
+
constructor(message: string) {
|
|
22
|
+
super(message);
|
|
23
|
+
|
|
24
|
+
this.name = NAME_ERROR_SCHEMATIC;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// #endregion
|
|
29
|
+
|
|
30
|
+
// #region Validated property
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The runtime representation of a parsed schema property, used internally during validation
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const parsed: ValidatedProperty = {
|
|
38
|
+
* key: 'age',
|
|
39
|
+
* required: true,
|
|
40
|
+
* types: ['number'],
|
|
41
|
+
* validators: { number: [(v) => v > 0] },
|
|
42
|
+
* };
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export type ValidatedProperty = {
|
|
46
|
+
/**
|
|
47
|
+
* The property name in the schema
|
|
48
|
+
*/
|
|
49
|
+
key: ValidatedPropertyKey;
|
|
50
|
+
/**
|
|
51
|
+
* Whether the property is required
|
|
52
|
+
*/
|
|
53
|
+
required: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* The allowed types for this property
|
|
56
|
+
*/
|
|
57
|
+
types: ValidatedPropertyType[];
|
|
58
|
+
/**
|
|
59
|
+
* Custom validators grouped by {@link ValueName}
|
|
60
|
+
*/
|
|
61
|
+
validators: ValidatedPropertyValidators;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Property name in schema
|
|
66
|
+
*/
|
|
67
|
+
export type ValidatedPropertyKey = {
|
|
68
|
+
/**
|
|
69
|
+
* Full property key, including parent keys for nested properties _(e.g., `address.street`)_
|
|
70
|
+
*/
|
|
71
|
+
full: string;
|
|
72
|
+
/**
|
|
73
|
+
* The last segment of the property key _(e.g., `street` for `address.street`)_
|
|
74
|
+
*/
|
|
75
|
+
short: string;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A union of valid types for a {@link ValidatedProperty}'s `types` array
|
|
80
|
+
*
|
|
81
|
+
* Can be a callback _(custom validator)_, a {@link Schematic}, a nested {@link ValidatedProperty}, or a {@link ValueName} string
|
|
82
|
+
*/
|
|
83
|
+
export type ValidatedPropertyType =
|
|
84
|
+
| GenericCallback
|
|
85
|
+
| ValidatedProperty[]
|
|
86
|
+
| Schematic<unknown>
|
|
87
|
+
| ValueName;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A map of validator functions keyed by {@link ValueName}, used at runtime in {@link ValidatedProperty}
|
|
91
|
+
*
|
|
92
|
+
* Each key holds an array of validator functions that receive an `unknown` value and return a `boolean`
|
|
93
|
+
*/
|
|
94
|
+
export type ValidatedPropertyValidators = {
|
|
95
|
+
[Key in ValueName]?: Array<(value: unknown) => boolean>;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// #endregion
|
|
99
|
+
|
|
100
|
+
// #region Property validation
|
|
101
|
+
|
|
102
|
+
export class ValidationError extends Error {
|
|
103
|
+
constructor(readonly information: ValidationInformation[]) {
|
|
104
|
+
super(
|
|
105
|
+
join(
|
|
106
|
+
information.map(item => item.message),
|
|
107
|
+
'; ',
|
|
108
|
+
),
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
this.name = NAME_ERROR_VALIDATION;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export type ValidationInformation = {
|
|
116
|
+
key: ValidationInformationKey;
|
|
117
|
+
message: string;
|
|
118
|
+
validator?: GenericCallback;
|
|
119
|
+
value: unknown;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export type ValidationInformationKey = ValidatedPropertyKey;
|
|
123
|
+
|
|
124
|
+
// #endregion
|