@oscarpalmer/jhunal 0.24.0 → 0.25.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 +4 -4
- package/dist/constants.mjs +4 -4
- package/dist/helpers/misc.helper.d.mts +3 -3
- package/dist/helpers/misc.helper.mjs +3 -3
- package/dist/index.d.mts +62 -62
- package/dist/index.mjs +17 -17
- package/dist/models/infer.model.d.mts +21 -21
- package/dist/models/misc.model.d.mts +3 -3
- package/dist/models/{schema.plain.model.d.mts → schematic.plain.model.d.mts} +18 -18
- package/dist/models/{schema.typed.model.d.mts → schematic.typed.model.d.mts} +8 -8
- package/dist/models/transform.model.d.mts +6 -6
- package/dist/models/validation.model.d.mts +2 -2
- package/dist/{schematic.d.mts → schema.d.mts} +18 -18
- package/dist/{schematic.mjs → schema.mjs} +12 -12
- package/dist/validator/object.validator.mjs +4 -4
- package/dist/validator/schematic.validator.d.mts +3 -3
- package/dist/validator/schematic.validator.mjs +4 -4
- package/package.json +1 -1
- package/src/constants.ts +3 -3
- package/src/helpers/misc.helper.ts +5 -5
- package/src/index.ts +4 -4
- package/src/models/infer.model.ts +26 -28
- package/src/models/misc.model.ts +3 -3
- package/src/models/{schema.plain.model.ts → schematic.plain.model.ts} +20 -20
- package/src/models/{schema.typed.model.ts → schematic.typed.model.ts} +8 -8
- package/src/models/transform.model.ts +6 -6
- package/src/models/validation.model.ts +2 -2
- package/src/{schematic.ts → schema.ts} +23 -23
- package/src/validator/object.validator.ts +4 -4
- package/src/validator/schematic.validator.ts +3 -3
- /package/dist/models/{schema.plain.model.mjs → schematic.plain.model.mjs} +0 -0
- /package/dist/models/{schema.typed.model.mjs → schematic.typed.model.mjs} +0 -0
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type {Constructor} from '@oscarpalmer/atoms/models';
|
|
2
|
-
import type {
|
|
2
|
+
import type {Schema} from '../schema';
|
|
3
3
|
import type {ExtractValueNames, ValueName, Values} from './misc.model';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* A generic
|
|
6
|
+
* A generic schematic allowing nested schematics, {@link SchematicEntry} values, or arrays of {@link SchematicEntry} as values
|
|
7
7
|
*/
|
|
8
|
-
export type
|
|
9
|
-
[key: string]:
|
|
8
|
+
export type PlainSchematic = {
|
|
9
|
+
[key: string]: PlainSchematic | SchematicEntry | SchematicEntry[] | undefined;
|
|
10
10
|
} & {
|
|
11
11
|
$default?: never;
|
|
12
12
|
$required?: never;
|
|
@@ -15,29 +15,29 @@ export type PlainSchema = {
|
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
* A
|
|
18
|
+
* A schematic for validating objects
|
|
19
19
|
*
|
|
20
20
|
* @example
|
|
21
21
|
* ```ts
|
|
22
|
-
* const
|
|
22
|
+
* const schematic = {
|
|
23
23
|
* name: 'string',
|
|
24
24
|
* age: 'number',
|
|
25
25
|
* tags: ['string', 'number'],
|
|
26
|
-
* };
|
|
26
|
+
* } satisfies Schematic;
|
|
27
27
|
* ```
|
|
28
28
|
*/
|
|
29
|
-
export type
|
|
29
|
+
export type Schematic = PlainSchematic;
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
|
-
* A union of all valid types for a single
|
|
32
|
+
* A union of all valid types for a single schematic entry
|
|
33
33
|
*
|
|
34
|
-
* Can be a {@link Constructor}, {@link
|
|
34
|
+
* Can be a {@link Constructor}, {@link PlainSchematic}, {@link SchematicProperty}, {@link Schema}, {@link ValueName}, or a custom validator function
|
|
35
35
|
*/
|
|
36
|
-
export type
|
|
36
|
+
export type SchematicEntry =
|
|
37
37
|
| Constructor
|
|
38
|
-
|
|
|
39
|
-
|
|
|
40
|
-
|
|
|
38
|
+
| PlainSchematic
|
|
39
|
+
| Schema<unknown>
|
|
40
|
+
| SchematicProperty
|
|
41
41
|
| ValueName
|
|
42
42
|
| ((value: unknown) => boolean);
|
|
43
43
|
|
|
@@ -46,7 +46,7 @@ export type SchemaEntry =
|
|
|
46
46
|
*
|
|
47
47
|
* @example
|
|
48
48
|
* ```ts
|
|
49
|
-
* const prop:
|
|
49
|
+
* const prop: SchematicProperty = {
|
|
50
50
|
* $required: false,
|
|
51
51
|
* $type: ['string', 'number'],
|
|
52
52
|
* $validators: {
|
|
@@ -56,7 +56,7 @@ export type SchemaEntry =
|
|
|
56
56
|
* };
|
|
57
57
|
* ```
|
|
58
58
|
*/
|
|
59
|
-
export type
|
|
59
|
+
export type SchematicProperty = {
|
|
60
60
|
$default?: unknown;
|
|
61
61
|
/**
|
|
62
62
|
* Whether the property is required _(defaults to `true`)_
|
|
@@ -73,14 +73,14 @@ export type SchemaProperty = {
|
|
|
73
73
|
};
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
|
-
* A union of valid types for a {@link
|
|
76
|
+
* A union of valid types for a {@link SchematicProperty}'s `$type` field
|
|
77
77
|
*
|
|
78
|
-
* Can be a {@link Constructor}, {@link
|
|
78
|
+
* Can be a {@link Constructor}, {@link PlainSchematic}, {@link Schema}, {@link ValueName} string, or a custom validator function
|
|
79
79
|
*/
|
|
80
80
|
export type SchemaPropertyType =
|
|
81
81
|
| Constructor
|
|
82
|
-
|
|
|
83
|
-
|
|
|
82
|
+
| PlainSchematic
|
|
83
|
+
| Schema<unknown>
|
|
84
84
|
| ValueName
|
|
85
85
|
| ((value: unknown) => boolean);
|
|
86
86
|
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type {PlainObject, Simplify} from '@oscarpalmer/atoms/models';
|
|
2
|
-
import type {
|
|
2
|
+
import type {Schema} from '../schema';
|
|
3
3
|
import type {OptionalKeys, RequiredKeys} from './misc.model';
|
|
4
|
-
import type {PropertyValidators} from './
|
|
4
|
+
import type {PropertyValidators} from './schematic.plain.model';
|
|
5
5
|
import type {ToSchemaPropertyType, ToSchemaType} from './transform.model';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* A typed optional property definition generated by {@link
|
|
8
|
+
* A typed optional property definition generated by {@link TypedSchematic} for optional keys, with `$required` set to `false` and excludes `undefined` from the type
|
|
9
9
|
*
|
|
10
10
|
* @template Value Property's type _(including `undefined`)_
|
|
11
11
|
*
|
|
@@ -24,7 +24,7 @@ export type TypedPropertyOptional<Value> = {
|
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
* A typed required property definition generated by {@link
|
|
27
|
+
* A typed required property definition generated by {@link TypedSchematic} for required keys, with `$required` defaulting to `true`
|
|
28
28
|
*
|
|
29
29
|
* @template Value Property's type
|
|
30
30
|
*
|
|
@@ -45,7 +45,7 @@ export type TypedPropertyRequired<Value> = {
|
|
|
45
45
|
/**
|
|
46
46
|
* Creates a schema type constrained to match a TypeScript type
|
|
47
47
|
*
|
|
48
|
-
* Required keys map to {@link ToSchemaType} or {@link TypedPropertyRequired}; plain object values may also use {@link
|
|
48
|
+
* Required keys map to {@link ToSchemaType} or {@link TypedPropertyRequired}; plain object values may also use {@link Schema}
|
|
49
49
|
*
|
|
50
50
|
* @template Model Object type to generate a schema for
|
|
51
51
|
*
|
|
@@ -60,14 +60,14 @@ export type TypedPropertyRequired<Value> = {
|
|
|
60
60
|
* };
|
|
61
61
|
* ```
|
|
62
62
|
*/
|
|
63
|
-
export type
|
|
63
|
+
export type TypedSchematic<Model extends PlainObject> = Simplify<
|
|
64
64
|
{
|
|
65
65
|
[Key in RequiredKeys<Model>]: Model[Key] extends PlainObject
|
|
66
|
-
?
|
|
66
|
+
? Schema<Model[Key]>
|
|
67
67
|
: ToSchemaType<Model[Key]> | TypedPropertyRequired<Model[Key]>;
|
|
68
68
|
} & {
|
|
69
69
|
[Key in OptionalKeys<Model>]: Exclude<Model[Key], undefined> extends PlainObject
|
|
70
|
-
?
|
|
70
|
+
? Schema<Exclude<Model[Key], undefined>>
|
|
71
71
|
: TypedPropertyOptional<Model[Key]>;
|
|
72
72
|
}
|
|
73
73
|
>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type {PlainObject} from '@oscarpalmer/atoms/models';
|
|
2
|
-
import type {
|
|
2
|
+
import type {Schema} from '../schema';
|
|
3
3
|
import type {DeduplicateTuple, UnionToTuple, UnwrapSingle, Values} from './misc.model';
|
|
4
|
-
import type {
|
|
4
|
+
import type {TypedSchematic} from './schematic.typed.model';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Maps each element of a tuple through {@link ToValueType}
|
|
@@ -36,12 +36,12 @@ export type ToSchemaPropertyType<Value> = UnwrapSingle<
|
|
|
36
36
|
/**
|
|
37
37
|
* Converts a single type to its schema property equivalent
|
|
38
38
|
*
|
|
39
|
-
* Plain objects become {@link
|
|
39
|
+
* Plain objects become {@link TypedSchematic}; primitives go through {@link ToValueType}
|
|
40
40
|
*
|
|
41
41
|
* @template Value Type to convert
|
|
42
42
|
*/
|
|
43
43
|
export type ToSchemaPropertyTypeEach<Value> = Value extends PlainObject
|
|
44
|
-
?
|
|
44
|
+
? TypedSchematic<Value>
|
|
45
45
|
: ToValueType<Value>;
|
|
46
46
|
|
|
47
47
|
/**
|
|
@@ -56,7 +56,7 @@ export type ToSchemaType<Value> = UnwrapSingle<
|
|
|
56
56
|
/**
|
|
57
57
|
* Maps a type to its {@link ValueName} string equivalent
|
|
58
58
|
*
|
|
59
|
-
* Resolves {@link
|
|
59
|
+
* Resolves {@link Schema} 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
|
|
60
60
|
*
|
|
61
61
|
* @template Value Type to map
|
|
62
62
|
*
|
|
@@ -68,7 +68,7 @@ export type ToSchemaType<Value> = UnwrapSingle<
|
|
|
68
68
|
* ```
|
|
69
69
|
*/
|
|
70
70
|
export type ToValueType<Value> =
|
|
71
|
-
Value extends
|
|
71
|
+
Value extends Schema<any>
|
|
72
72
|
? Value
|
|
73
73
|
: {
|
|
74
74
|
[Key in keyof Omit<Values, 'object'>]: Value extends Values[Key] ? Key : never;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type {GenericCallback, PlainObject} from '@oscarpalmer/atoms/models';
|
|
2
2
|
import {join} from '@oscarpalmer/atoms/string';
|
|
3
3
|
import {NAME_ERROR_SCHEMATIC, NAME_ERROR_VALIDATION} from '../constants';
|
|
4
|
-
import type {
|
|
4
|
+
import type {Schema} from '../schema';
|
|
5
5
|
import type {ValueName} from './misc.model';
|
|
6
6
|
|
|
7
7
|
// #region Named validation
|
|
@@ -147,6 +147,6 @@ export type ValidatorParameters = {
|
|
|
147
147
|
strict: boolean;
|
|
148
148
|
};
|
|
149
149
|
|
|
150
|
-
export type ValidatorType = Function | PlainObject |
|
|
150
|
+
export type ValidatorType = Function | PlainObject | Schema<unknown> | ValueName;
|
|
151
151
|
|
|
152
152
|
// #endregion
|
|
@@ -2,11 +2,11 @@ import {isPlainObject} from '@oscarpalmer/atoms/is';
|
|
|
2
2
|
import type {PlainObject} from '@oscarpalmer/atoms/models';
|
|
3
3
|
import {error, ok} from '@oscarpalmer/atoms/result/misc';
|
|
4
4
|
import type {Result} from '@oscarpalmer/atoms/result/models';
|
|
5
|
-
import {
|
|
6
|
-
import {getParameters,
|
|
5
|
+
import {PROPERTY_SCHEMA, SCHEMATIC_MESSAGE_SCHEMA_INVALID_TYPE} from './constants';
|
|
6
|
+
import {getParameters, isSchema} from './helpers/misc.helper';
|
|
7
7
|
import type {Infer} from './models/infer.model';
|
|
8
|
-
import type {
|
|
9
|
-
import type {
|
|
8
|
+
import type {Schematic} from './models/schematic.plain.model';
|
|
9
|
+
import type {TypedSchematic} from './models/schematic.typed.model';
|
|
10
10
|
import {
|
|
11
11
|
SchematicError,
|
|
12
12
|
type GetOptions,
|
|
@@ -17,21 +17,21 @@ import {
|
|
|
17
17
|
import {getObjectValidator} from './validator/object.validator';
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
|
-
* A
|
|
20
|
+
* A schema for validating objects
|
|
21
21
|
*/
|
|
22
|
-
export class
|
|
23
|
-
declare private readonly $
|
|
22
|
+
export class Schema<Model> {
|
|
23
|
+
declare private readonly $schema: true;
|
|
24
24
|
|
|
25
25
|
#validator: Validator;
|
|
26
26
|
|
|
27
27
|
constructor(validator: Validator) {
|
|
28
|
-
Object.defineProperty(this,
|
|
28
|
+
Object.defineProperty(this, PROPERTY_SCHEMA, {
|
|
29
29
|
value: true,
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
this.#validator = validator;
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
schemaValidators.set(this, validator);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/**
|
|
@@ -230,25 +230,25 @@ export class Schematic<Model> {
|
|
|
230
230
|
}
|
|
231
231
|
|
|
232
232
|
/**
|
|
233
|
-
* Create a
|
|
233
|
+
* Create a schema from a schematic
|
|
234
234
|
* @template Model Schema type
|
|
235
|
-
* @param schema
|
|
236
|
-
* @throws Throws {@link SchematicError} if the
|
|
237
|
-
* @returns A
|
|
235
|
+
* @param schema Schematic to create the schema from
|
|
236
|
+
* @throws Throws {@link SchematicError} if the schematic can not be converted into a schema
|
|
237
|
+
* @returns A schema for the given schematic
|
|
238
238
|
*/
|
|
239
|
-
export function
|
|
239
|
+
export function schema<Model extends Schematic>(schema: Model): Schema<Infer<Model>>;
|
|
240
240
|
|
|
241
241
|
/**
|
|
242
|
-
* Create a
|
|
242
|
+
* Create a schema from a typed schematic
|
|
243
243
|
* @template Model Existing type
|
|
244
|
-
* @param schema Typed
|
|
245
|
-
* @throws Throws {@link SchematicError} if the
|
|
246
|
-
* @returns A
|
|
244
|
+
* @param schema Typed schematic to create the schema from
|
|
245
|
+
* @throws Throws {@link SchematicError} if the schematic can not be converted into a schema
|
|
246
|
+
* @returns A schema for the given typed schematic
|
|
247
247
|
*/
|
|
248
|
-
export function
|
|
248
|
+
export function schema<Model extends PlainObject>(schema: TypedSchematic<Model>): Schema<Model>;
|
|
249
249
|
|
|
250
|
-
export function
|
|
251
|
-
if (
|
|
250
|
+
export function schema<Model extends Schematic>(schema: Model): Schema<Model> {
|
|
251
|
+
if (isSchema(schema)) {
|
|
252
252
|
return schema;
|
|
253
253
|
}
|
|
254
254
|
|
|
@@ -256,7 +256,7 @@ export function schematic<Model extends Schema>(schema: Model): Schematic<Model>
|
|
|
256
256
|
throw new SchematicError(SCHEMATIC_MESSAGE_SCHEMA_INVALID_TYPE);
|
|
257
257
|
}
|
|
258
258
|
|
|
259
|
-
return new
|
|
259
|
+
return new Schema<Model>(getObjectValidator(schema));
|
|
260
260
|
}
|
|
261
261
|
|
|
262
|
-
export const
|
|
262
|
+
export const schemaValidators = new WeakMap<Schema<unknown>, Validator>();
|
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
getSchematicPropertyTypeMessage,
|
|
24
24
|
getUnknownKeysMessage,
|
|
25
25
|
} from '../helpers/message.helper';
|
|
26
|
-
import {getParameters,
|
|
26
|
+
import {getParameters, isSchema} from '../helpers/misc.helper';
|
|
27
27
|
import type {ValueName} from '../models/misc.model';
|
|
28
28
|
import {
|
|
29
29
|
type NamedValidatorHandlers,
|
|
@@ -40,7 +40,7 @@ import {getBaseValidator} from './base.validator';
|
|
|
40
40
|
import {getFunctionValidator} from './function.validator';
|
|
41
41
|
import {getNamedHandlers} from './named.handler';
|
|
42
42
|
import {getNamedValidator} from './named.validator';
|
|
43
|
-
import {
|
|
43
|
+
import {getSchemaValidator} from './schematic.validator';
|
|
44
44
|
|
|
45
45
|
function getDefaults(
|
|
46
46
|
obj: PlainObject,
|
|
@@ -161,8 +161,8 @@ export function getObjectValidator(
|
|
|
161
161
|
validator = getObjectValidator(type, fullKey, typed);
|
|
162
162
|
break;
|
|
163
163
|
|
|
164
|
-
case
|
|
165
|
-
validator =
|
|
164
|
+
case isSchema(type):
|
|
165
|
+
validator = getSchemaValidator(type);
|
|
166
166
|
break;
|
|
167
167
|
|
|
168
168
|
case TYPE_ALL.has(type as ValueName):
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {isPlainObject} from '@oscarpalmer/atoms/is';
|
|
2
2
|
import type {Validator} from '../models/validation.model';
|
|
3
|
-
import {
|
|
3
|
+
import {Schema, schemaValidators} from '../schema';
|
|
4
4
|
|
|
5
|
-
export function
|
|
6
|
-
const validator =
|
|
5
|
+
export function getSchemaValidator(schematic: Schema<unknown>): Validator {
|
|
6
|
+
const validator = schemaValidators.get(schematic)!;
|
|
7
7
|
|
|
8
8
|
return (input, parameters, get) => {
|
|
9
9
|
let result: ReturnType<Validator>;
|
|
File without changes
|
|
File without changes
|