@oscarpalmer/jhunal 0.7.0 → 0.8.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.js +4 -3
- package/dist/index.js +2 -1
- package/dist/is.js +10 -6
- package/dist/jhunal.full.js +632 -48
- package/dist/schematic.js +12 -7
- package/dist/validation/schema.validation.js +23 -17
- package/dist/validation/value.validation.js +6 -5
- package/package.json +8 -8
- package/src/constants.ts +6 -2
- package/src/index.ts +1 -0
- package/src/is.ts +12 -7
- package/src/models.ts +112 -54
- package/src/schematic.ts +6 -7
- package/src/validation/schema.validation.ts +30 -19
- package/src/validation/value.validation.ts +12 -6
- package/types/constants.d.ts +3 -0
- package/types/index.d.ts +1 -0
- package/types/is.d.ts +3 -1
- package/types/models.d.ts +32 -24
- package/types/schematic.d.ts +1 -1
- package/types/validation/schema.validation.d.ts +1 -1
package/types/is.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { Constructor } from './models';
|
|
1
2
|
import type { Schematic } from './schematic';
|
|
2
|
-
export declare function
|
|
3
|
+
export declare function isConstructor(value: unknown): value is Constructor<unknown>;
|
|
4
|
+
export declare function isInstance<Instance>(constructor: Constructor<Instance>): (value: unknown) => value is Instance;
|
|
3
5
|
export declare function isSchematic(value: unknown): value is Schematic<never>;
|
package/types/models.d.ts
CHANGED
|
@@ -1,25 +1,32 @@
|
|
|
1
1
|
import type { PlainObject, Simplify } from '@oscarpalmer/atoms/models';
|
|
2
2
|
import type { Schematic } from './schematic';
|
|
3
|
+
export type Constructor<Instance = any> = new (...args: any[]) => Instance;
|
|
4
|
+
type DeduplicateTuple<Value extends unknown[], Seen extends unknown[] = []> = Value extends [
|
|
5
|
+
infer Head,
|
|
6
|
+
...infer Tail
|
|
7
|
+
] ? Head extends Seen[number] ? DeduplicateTuple<Tail, Seen> : DeduplicateTuple<Tail, [...Seen, Head]> : Seen;
|
|
3
8
|
/**
|
|
4
9
|
* Infer the TypeScript type from a schema definition
|
|
5
10
|
*/
|
|
6
11
|
export type Infer<Model extends Schema> = Simplify<{
|
|
7
|
-
[Key in InferRequiredKeys<Model>]:
|
|
12
|
+
[Key in InferRequiredKeys<Model>]: InferSchemaEntry<Model[Key]>;
|
|
8
13
|
} & {
|
|
9
|
-
[Key in InferOptionalKeys<Model>]?:
|
|
14
|
+
[Key in InferOptionalKeys<Model>]?: InferSchemaEntry<Model[Key]>;
|
|
10
15
|
}>;
|
|
11
|
-
type InferEntryValue<Value> = Value extends ValueName ? Values[Value] : Value extends ValueName[] ? Values[Value[number]] : never;
|
|
12
16
|
type InferOptionalKeys<Model extends Schema> = keyof {
|
|
13
17
|
[Key in keyof Model as IsOptionalProperty<Model[Key]> extends true ? Key : never]: never;
|
|
14
18
|
};
|
|
15
|
-
type InferPropertyType<Value> = Value extends (infer Item)[] ?
|
|
16
|
-
type InferPropertyValue<
|
|
19
|
+
type InferPropertyType<Value> = Value extends (infer Item)[] ? InferPropertyValue<Item> : InferPropertyValue<Value>;
|
|
20
|
+
type InferPropertyValue<Value> = Value extends Constructor<infer Instance> ? Instance : Value extends Schematic<infer Model> ? Model : Value extends ValueName ? Values[Value & ValueName] : Value extends Schema ? Infer<Value> : never;
|
|
17
21
|
type InferRequiredKeys<Model extends Schema> = keyof {
|
|
18
22
|
[Key in keyof Model as IsOptionalProperty<Model[Key]> extends true ? never : Key]: never;
|
|
19
23
|
};
|
|
20
|
-
type
|
|
24
|
+
type InferSchemaEntry<Value> = Value extends (infer Item)[] ? InferSchemaEntryValue<Item> : InferSchemaEntryValue<Value>;
|
|
25
|
+
type InferSchemaEntryValue<Value> = Value extends Constructor<infer Instance> ? Instance : Value extends Schematic<infer Model> ? Model : Value extends SchemaProperty ? InferPropertyType<Value['$type']> : Value extends ValueName ? Values[Value & ValueName] : Value extends Schema ? Infer<Value> : never;
|
|
21
26
|
type IsOptionalProperty<Value> = Value extends SchemaProperty ? Value['$required'] extends false ? true : false : false;
|
|
22
27
|
type LastOfUnion<Value> = UnionToIntersection<Value extends unknown ? () => Value : never> extends () => infer Item ? Item : never;
|
|
28
|
+
type MapToValueTypes<Value extends unknown[]> = Value extends [infer Head, ...infer Tail] ? [ToValueType<Head>, ...MapToValueTypes<Tail>] : [];
|
|
29
|
+
type MapToSchemaPropertyTypes<Value extends unknown[]> = Value extends [infer Head, ...infer Tail] ? [ToSchemaPropertyTypeEach<Head>, ...MapToSchemaPropertyTypes<Tail>] : [];
|
|
23
30
|
/**
|
|
24
31
|
* A nested schema with optional requirement flag
|
|
25
32
|
*/
|
|
@@ -34,9 +41,9 @@ type RequiredKeys<Value> = Exclude<keyof Value, OptionalKeys<Value>>;
|
|
|
34
41
|
* A schema for validating objects
|
|
35
42
|
*/
|
|
36
43
|
export type Schema = SchemaIndex;
|
|
37
|
-
type SchemaEntry = NestedSchema | SchemaProperty | Schematic<unknown> | ValueName
|
|
44
|
+
type SchemaEntry = Constructor | NestedSchema | SchemaProperty | Schematic<unknown> | ValueName;
|
|
38
45
|
interface SchemaIndex {
|
|
39
|
-
[key: string]: SchemaEntry;
|
|
46
|
+
[key: string]: SchemaEntry | SchemaEntry[];
|
|
40
47
|
}
|
|
41
48
|
/**
|
|
42
49
|
* A property definition with explicit type(s) and optional requirement flag
|
|
@@ -45,18 +52,21 @@ export type SchemaProperty = {
|
|
|
45
52
|
$required?: boolean;
|
|
46
53
|
$type: SchemaPropertyType | SchemaPropertyType[];
|
|
47
54
|
};
|
|
48
|
-
type SchemaPropertyType = Schema | Schematic<unknown> | ValueName;
|
|
49
|
-
type ToSchemaPropertyType<Value> = UnwrapSingle<UnionToTuple<
|
|
50
|
-
type ToSchemaPropertyTypeEach<Value> = Value extends PlainObject ? TypedSchema<Value> :
|
|
51
|
-
type ToSchemaType<Value> = UnwrapSingle<UnionToTuple<
|
|
52
|
-
type
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
type SchemaPropertyType = Constructor | Schema | Schematic<unknown> | ValueName;
|
|
56
|
+
type ToSchemaPropertyType<Value> = UnwrapSingle<DeduplicateTuple<MapToSchemaPropertyTypes<UnionToTuple<Value>>>>;
|
|
57
|
+
type ToSchemaPropertyTypeEach<Value> = Value extends PlainObject ? TypedSchema<Value> : ToValueType<Value>;
|
|
58
|
+
type ToSchemaType<Value> = UnwrapSingle<DeduplicateTuple<MapToValueTypes<UnionToTuple<Value>>>>;
|
|
59
|
+
type ToValueType<Value> = Value extends unknown[] ? 'array' : Value extends bigint ? 'bigint' : Value extends boolean ? 'boolean' : Value extends Date ? 'date' : Value extends Function ? 'function' : Value extends null ? 'null' : Value extends number ? 'number' : Value extends object ? 'object' | ((value: unknown) => value is Value) : Value extends string ? 'string' : Value extends symbol ? 'symbol' : Value extends undefined ? 'undefined' : (value: unknown) => value is Value;
|
|
60
|
+
type TuplePermutations<Tuple extends unknown[], Elput extends unknown[] = []> = Tuple['length'] extends 0 ? Elput : {
|
|
61
|
+
[Key in keyof Tuple]: TuplePermutations<TupleRemoveAt<Tuple, Key & `${number}`>, [
|
|
62
|
+
...Elput,
|
|
63
|
+
Tuple[Key]
|
|
64
|
+
]>;
|
|
65
|
+
}[keyof Tuple & `${number}`];
|
|
66
|
+
type TupleRemoveAt<Items extends unknown[], Item extends string, Prefix extends unknown[] = []> = Items extends [infer Head, ...infer Tail] ? `${Prefix['length']}` extends Item ? [...Prefix, ...Tail] : TupleRemoveAt<Tail, Item, [...Prefix, Head]> : Prefix;
|
|
57
67
|
export type TypedPropertyOptional<Value> = {
|
|
58
68
|
$required: false;
|
|
59
|
-
$type: ToSchemaPropertyType<Value
|
|
69
|
+
$type: ToSchemaPropertyType<Exclude<Value, undefined>>;
|
|
60
70
|
};
|
|
61
71
|
export type TypedPropertyRequired<Value> = {
|
|
62
72
|
$required?: true;
|
|
@@ -74,17 +84,18 @@ type TypedSchemaOptional<Model extends PlainObject> = {
|
|
|
74
84
|
$required: false;
|
|
75
85
|
} & TypedSchema<Model>;
|
|
76
86
|
type TypedSchemaRequired<Model extends PlainObject> = {
|
|
77
|
-
$required?:
|
|
87
|
+
$required?: true;
|
|
78
88
|
} & TypedSchema<Model>;
|
|
79
|
-
type UnionToIntersection<Value> = (Value extends unknown ? (
|
|
89
|
+
type UnionToIntersection<Value> = (Value extends unknown ? (value: Value) => void : never) extends (value: infer Item) => void ? Item : never;
|
|
80
90
|
type UnionToTuple<Value, Items extends unknown[] = []> = [Value] extends [never] ? Items : UnionToTuple<Exclude<Value, LastOfUnion<Value>>, [LastOfUnion<Value>, ...Items]>;
|
|
81
|
-
type UnwrapSingle<Value extends unknown[]> = Value extends [infer Only] ? Only : Value;
|
|
91
|
+
type UnwrapSingle<Value extends unknown[]> = Value extends [infer Only] ? Only : Value['length'] extends 1 | 2 | 3 | 4 | 5 ? TuplePermutations<Value> : Value;
|
|
82
92
|
export type ValidatedProperty = {
|
|
83
93
|
required: boolean;
|
|
84
94
|
types: ValidatedPropertyType[];
|
|
85
95
|
};
|
|
86
96
|
export type ValidatedPropertyType = Schematic<unknown> | ValueName;
|
|
87
97
|
export type ValidatedSchema = {
|
|
98
|
+
enabled: boolean;
|
|
88
99
|
keys: {
|
|
89
100
|
array: string[];
|
|
90
101
|
set: Set<string>;
|
|
@@ -95,7 +106,6 @@ export type ValidatedSchema = {
|
|
|
95
106
|
* Valid type name strings
|
|
96
107
|
*/
|
|
97
108
|
export type ValueName = keyof Values;
|
|
98
|
-
type ValueNameFallbacks = 'date-like' | 'numerical';
|
|
99
109
|
/**
|
|
100
110
|
* Map of type names to their TypeScript/validatable equivalents
|
|
101
111
|
*/
|
|
@@ -104,11 +114,9 @@ export type Values = {
|
|
|
104
114
|
bigint: bigint;
|
|
105
115
|
boolean: boolean;
|
|
106
116
|
date: Date;
|
|
107
|
-
'date-like': number | string | Date;
|
|
108
117
|
function: Function;
|
|
109
118
|
null: null;
|
|
110
119
|
number: number;
|
|
111
|
-
numerical: bigint | number;
|
|
112
120
|
object: object;
|
|
113
121
|
string: string;
|
|
114
122
|
symbol: symbol;
|
package/types/schematic.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type { Infer, Schema, TypedSchema } from './models';
|
|
|
6
6
|
export declare class Schematic<Model> {
|
|
7
7
|
#private;
|
|
8
8
|
private readonly $schematic;
|
|
9
|
-
get
|
|
9
|
+
get enabled(): boolean;
|
|
10
10
|
constructor(schema: Model);
|
|
11
11
|
/**
|
|
12
12
|
* Does the value match the schema?
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { ValidatedSchema } from '../models';
|
|
2
|
-
export declare function
|
|
2
|
+
export declare function getSchema(schema: unknown): ValidatedSchema;
|