@oscarpalmer/jhunal 0.4.0 → 0.5.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/helpers.js +35 -37
- package/dist/index.js +2 -1
- package/dist/is.js +4 -9
- package/dist/jhunal.full.js +134 -0
- package/dist/model.js +0 -1
- package/dist/schematic.js +19 -11
- package/dist/validation/schema.validation.js +32 -45
- package/dist/validation/value.validation.js +37 -29
- package/package.json +15 -19
- package/src/index.ts +2 -2
- package/src/is.ts +1 -1
- package/src/model.ts +99 -82
- package/src/schematic.ts +36 -18
- package/src/validation/value.validation.ts +34 -11
- package/types/index.d.ts +2 -2
- package/types/is.d.ts +1 -1
- package/types/model.d.ts +29 -27
- package/types/schematic.d.ts +14 -1
- package/types/validation/value.validation.d.ts +2 -1
- package/dist/helpers.cjs +0 -45
- package/dist/index.cjs +0 -9
- package/dist/is.cjs +0 -19
- package/dist/model.cjs +0 -2
- package/dist/schematic.cjs +0 -17
- package/dist/validation/schema.validation.cjs +0 -54
- package/dist/validation/type.validation.cjs +0 -33
- package/dist/validation/type.validation.js +0 -29
- package/dist/validation/value.validation.cjs +0 -35
- package/src/validation/type.validation.ts +0 -34
- package/types/helpers.d.cts +0 -43
- package/types/index.d.cts +0 -360
- package/types/is.d.cts +0 -15
- package/types/model.d.cts +0 -365
- package/types/schematic.d.cts +0 -360
- package/types/validation/schema.validation.d.cts +0 -55
- package/types/validation/type.validation.d.cts +0 -43
- package/types/validation/type.validation.d.ts +0 -2
- package/types/validation/value.validation.d.cts +0 -43
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
|
|
5
|
-
const is = require('../is.cjs');
|
|
6
|
-
const validation_value_validation = require('./value.validation.cjs');
|
|
7
|
-
|
|
8
|
-
function validateType(type, value) {
|
|
9
|
-
if (typeof type === "string") {
|
|
10
|
-
return validators[type](value);
|
|
11
|
-
}
|
|
12
|
-
if (is.isSchematic(type)) {
|
|
13
|
-
return type.is(value);
|
|
14
|
-
}
|
|
15
|
-
return validation_value_validation.validateValue(type, value);
|
|
16
|
-
}
|
|
17
|
-
const validators = {
|
|
18
|
-
array: Array.isArray,
|
|
19
|
-
bigint: (value) => typeof value === "bigint",
|
|
20
|
-
boolean: (value) => typeof value === "boolean",
|
|
21
|
-
date: (value) => value instanceof Date,
|
|
22
|
-
"date-like": is.isDateLike,
|
|
23
|
-
function: (value) => typeof value === "function",
|
|
24
|
-
null: (value) => value === null,
|
|
25
|
-
number: (value) => typeof value === "number",
|
|
26
|
-
numerical: (value) => validators.bigint(value) || validators.number(value),
|
|
27
|
-
object: (value) => typeof value === "object" && value !== null,
|
|
28
|
-
string: (value) => typeof value === "string",
|
|
29
|
-
symbol: (value) => typeof value === "symbol",
|
|
30
|
-
undefined: (value) => value === void 0
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
exports.validateType = validateType;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { isDateLike, isSchematic } from '../is.js';
|
|
2
|
-
import { validateValue } from './value.validation.js';
|
|
3
|
-
|
|
4
|
-
function validateType(type, value) {
|
|
5
|
-
if (typeof type === "string") {
|
|
6
|
-
return validators[type](value);
|
|
7
|
-
}
|
|
8
|
-
if (isSchematic(type)) {
|
|
9
|
-
return type.is(value);
|
|
10
|
-
}
|
|
11
|
-
return validateValue(type, value);
|
|
12
|
-
}
|
|
13
|
-
const validators = {
|
|
14
|
-
array: Array.isArray,
|
|
15
|
-
bigint: (value) => typeof value === "bigint",
|
|
16
|
-
boolean: (value) => typeof value === "boolean",
|
|
17
|
-
date: (value) => value instanceof Date,
|
|
18
|
-
"date-like": isDateLike,
|
|
19
|
-
function: (value) => typeof value === "function",
|
|
20
|
-
null: (value) => value === null,
|
|
21
|
-
number: (value) => typeof value === "number",
|
|
22
|
-
numerical: (value) => validators.bigint(value) || validators.number(value),
|
|
23
|
-
object: (value) => typeof value === "object" && value !== null,
|
|
24
|
-
string: (value) => typeof value === "string",
|
|
25
|
-
symbol: (value) => typeof value === "symbol",
|
|
26
|
-
undefined: (value) => value === void 0
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export { validateType };
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
|
|
5
|
-
const validation_type_validation = require('./type.validation.cjs');
|
|
6
|
-
|
|
7
|
-
function validateValue(validated, obj) {
|
|
8
|
-
if (typeof obj !== "object" || obj === null) {
|
|
9
|
-
return false;
|
|
10
|
-
}
|
|
11
|
-
outer: for (let keyIndex = 0; keyIndex < validated.length; keyIndex += 1) {
|
|
12
|
-
const key = validated.keys[keyIndex];
|
|
13
|
-
const property = validated.properties[key];
|
|
14
|
-
const value = obj[key];
|
|
15
|
-
if (value === void 0 && property.required && !property.types.includes("undefined")) {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
const typesLength = property.types.length;
|
|
19
|
-
if (typesLength === 1) {
|
|
20
|
-
if (!validation_type_validation.validateType(property.types[0], value)) {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
continue;
|
|
24
|
-
}
|
|
25
|
-
for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
|
|
26
|
-
if (validation_type_validation.validateType(property.types[typeIndex], value)) {
|
|
27
|
-
continue outer;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
return true;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
exports.validateValue = validateValue;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import {isDateLike, isSchematic} from '../is';
|
|
2
|
-
import type {ValidatedPropertyType, ValidatedSchema, Values} from '../model';
|
|
3
|
-
import {validateValue} from './value.validation';
|
|
4
|
-
|
|
5
|
-
export function validateType(
|
|
6
|
-
type: ValidatedPropertyType,
|
|
7
|
-
value: unknown,
|
|
8
|
-
): boolean {
|
|
9
|
-
if (typeof type === 'string') {
|
|
10
|
-
return validators[type](value);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (isSchematic(type)) {
|
|
14
|
-
return type.is(value);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return validateValue(type as ValidatedSchema, value);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const validators: Record<keyof Values, (value: unknown) => boolean> = {
|
|
21
|
-
array: Array.isArray,
|
|
22
|
-
bigint: value => typeof value === 'bigint',
|
|
23
|
-
boolean: value => typeof value === 'boolean',
|
|
24
|
-
date: value => value instanceof Date,
|
|
25
|
-
'date-like': isDateLike,
|
|
26
|
-
function: value => typeof value === 'function',
|
|
27
|
-
null: value => value === null,
|
|
28
|
-
number: value => typeof value === 'number',
|
|
29
|
-
numerical: value => validators.bigint(value) || validators.number(value),
|
|
30
|
-
object: value => typeof value === 'object' && value !== null,
|
|
31
|
-
string: value => typeof value === 'string',
|
|
32
|
-
symbol: value => typeof value === 'symbol',
|
|
33
|
-
undefined: value => value === undefined,
|
|
34
|
-
};
|
package/types/helpers.d.cts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
// Generated by dts-bundle-generator v9.5.1
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A schematic for validating objects
|
|
5
|
-
*/
|
|
6
|
-
export type Schematic<Model> = {
|
|
7
|
-
/**
|
|
8
|
-
* Does the value match the schema?
|
|
9
|
-
*/
|
|
10
|
-
is(value: unknown): value is Model;
|
|
11
|
-
};
|
|
12
|
-
export type ValidatedProperty = {
|
|
13
|
-
required: boolean;
|
|
14
|
-
types: ValidatedPropertyType[];
|
|
15
|
-
};
|
|
16
|
-
export type ValidatedPropertyType = Schematic<unknown> | ValidatedSchema | ValueKey;
|
|
17
|
-
export type ValidatedSchema = {
|
|
18
|
-
keys: string[];
|
|
19
|
-
length: number;
|
|
20
|
-
properties: ValidatedSchemaProperties;
|
|
21
|
-
};
|
|
22
|
-
export type ValidatedSchemaProperties = {
|
|
23
|
-
[key: string]: ValidatedProperty;
|
|
24
|
-
};
|
|
25
|
-
export type ValueKey = keyof Values;
|
|
26
|
-
export type Values = {
|
|
27
|
-
array: unknown[];
|
|
28
|
-
bigint: bigint;
|
|
29
|
-
boolean: boolean;
|
|
30
|
-
date: Date;
|
|
31
|
-
"date-like": number | string | Date;
|
|
32
|
-
function: Function;
|
|
33
|
-
null: null;
|
|
34
|
-
number: number;
|
|
35
|
-
numerical: bigint | number;
|
|
36
|
-
object: object;
|
|
37
|
-
string: string;
|
|
38
|
-
symbol: symbol;
|
|
39
|
-
undefined: undefined;
|
|
40
|
-
};
|
|
41
|
-
export declare function getTypes(value: unknown): ValidatedPropertyType[];
|
|
42
|
-
|
|
43
|
-
export {};
|
package/types/index.d.cts
DELETED
|
@@ -1,360 +0,0 @@
|
|
|
1
|
-
// Generated by dts-bundle-generator v9.5.1
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
5
|
-
|
|
6
|
-
Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
|
|
7
|
-
|
|
8
|
-
@example
|
|
9
|
-
```
|
|
10
|
-
import type {UnionToIntersection} from 'type-fest';
|
|
11
|
-
|
|
12
|
-
type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};
|
|
13
|
-
|
|
14
|
-
type Intersection = UnionToIntersection<Union>;
|
|
15
|
-
//=> {the(): void; great(arg: string): void; escape: boolean};
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
A more applicable example which could make its way into your library code follows.
|
|
19
|
-
|
|
20
|
-
@example
|
|
21
|
-
```
|
|
22
|
-
import type {UnionToIntersection} from 'type-fest';
|
|
23
|
-
|
|
24
|
-
class CommandOne {
|
|
25
|
-
commands: {
|
|
26
|
-
a1: () => undefined,
|
|
27
|
-
b1: () => undefined,
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
class CommandTwo {
|
|
32
|
-
commands: {
|
|
33
|
-
a2: (argA: string) => undefined,
|
|
34
|
-
b2: (argB: string) => undefined,
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const union = [new CommandOne(), new CommandTwo()].map(instance => instance.commands);
|
|
39
|
-
type Union = typeof union;
|
|
40
|
-
//=> {a1(): void; b1(): void} | {a2(argA: string): void; b2(argB: string): void}
|
|
41
|
-
|
|
42
|
-
type Intersection = UnionToIntersection<Union>;
|
|
43
|
-
//=> {a1(): void; b1(): void; a2(argA: string): void; b2(argB: string): void}
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
@category Type
|
|
47
|
-
*/
|
|
48
|
-
export type UnionToIntersection<Union> = (
|
|
49
|
-
// `extends unknown` is always going to be the case and is used to convert the
|
|
50
|
-
// `Union` into a [distributive conditional
|
|
51
|
-
// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
52
|
-
Union extends unknown ? (distributedUnion: Union) => void : never) extends ((mergedIntersection: infer Intersection) => void) ? Intersection & Union : never;
|
|
53
|
-
/**
|
|
54
|
-
Extract all optional keys from the given type.
|
|
55
|
-
|
|
56
|
-
This is useful when you want to create a new type that contains different type values for the optional keys only.
|
|
57
|
-
|
|
58
|
-
@example
|
|
59
|
-
```
|
|
60
|
-
import type {OptionalKeysOf, Except} from 'type-fest';
|
|
61
|
-
|
|
62
|
-
interface User {
|
|
63
|
-
name: string;
|
|
64
|
-
surname: string;
|
|
65
|
-
|
|
66
|
-
luckyNumber?: number;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const REMOVE_FIELD = Symbol('remove field symbol');
|
|
70
|
-
type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
|
|
71
|
-
[Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const update1: UpdateOperation<User> = {
|
|
75
|
-
name: 'Alice'
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
const update2: UpdateOperation<User> = {
|
|
79
|
-
name: 'Bob',
|
|
80
|
-
luckyNumber: REMOVE_FIELD
|
|
81
|
-
};
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
@category Utilities
|
|
85
|
-
*/
|
|
86
|
-
export type OptionalKeysOf<BaseType extends object> = BaseType extends unknown // For distributing `BaseType`
|
|
87
|
-
? (keyof {
|
|
88
|
-
[Key in keyof BaseType as BaseType extends Record<Key, BaseType[Key]> ? never : Key]: never;
|
|
89
|
-
}) & (keyof BaseType) // Intersect with `keyof BaseType` to ensure result of `OptionalKeysOf<BaseType>` is always assignable to `keyof BaseType`
|
|
90
|
-
: never; // Should never happen
|
|
91
|
-
/**
|
|
92
|
-
Extract all required keys from the given type.
|
|
93
|
-
|
|
94
|
-
This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
|
|
95
|
-
|
|
96
|
-
@example
|
|
97
|
-
```
|
|
98
|
-
import type {RequiredKeysOf} from 'type-fest';
|
|
99
|
-
|
|
100
|
-
declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
|
|
101
|
-
|
|
102
|
-
interface User {
|
|
103
|
-
name: string;
|
|
104
|
-
surname: string;
|
|
105
|
-
|
|
106
|
-
luckyNumber?: number;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const validator1 = createValidation<User>('name', value => value.length < 25);
|
|
110
|
-
const validator2 = createValidation<User>('surname', value => value.length < 25);
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
@category Utilities
|
|
114
|
-
*/
|
|
115
|
-
export type RequiredKeysOf<BaseType extends object> = BaseType extends unknown // For distributing `BaseType`
|
|
116
|
-
? Exclude<keyof BaseType, OptionalKeysOf<BaseType>> : never; // Should never happen
|
|
117
|
-
/**
|
|
118
|
-
Returns a boolean for whether the given type is `never`.
|
|
119
|
-
|
|
120
|
-
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
|
121
|
-
@link https://stackoverflow.com/a/53984913/10292952
|
|
122
|
-
@link https://www.zhenghao.io/posts/ts-never
|
|
123
|
-
|
|
124
|
-
Useful in type utilities, such as checking if something does not occur.
|
|
125
|
-
|
|
126
|
-
@example
|
|
127
|
-
```
|
|
128
|
-
import type {IsNever, And} from 'type-fest';
|
|
129
|
-
|
|
130
|
-
// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
|
|
131
|
-
type AreStringsEqual<A extends string, B extends string> =
|
|
132
|
-
And<
|
|
133
|
-
IsNever<Exclude<A, B>> extends true ? true : false,
|
|
134
|
-
IsNever<Exclude<B, A>> extends true ? true : false
|
|
135
|
-
>;
|
|
136
|
-
|
|
137
|
-
type EndIfEqual<I extends string, O extends string> =
|
|
138
|
-
AreStringsEqual<I, O> extends true
|
|
139
|
-
? never
|
|
140
|
-
: void;
|
|
141
|
-
|
|
142
|
-
function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
|
|
143
|
-
if (input === output) {
|
|
144
|
-
process.exit(0);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
endIfEqual('abc', 'abc');
|
|
149
|
-
//=> never
|
|
150
|
-
|
|
151
|
-
endIfEqual('abc', '123');
|
|
152
|
-
//=> void
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
@category Type Guard
|
|
156
|
-
@category Utilities
|
|
157
|
-
*/
|
|
158
|
-
export type IsNever<T> = [
|
|
159
|
-
T
|
|
160
|
-
] extends [
|
|
161
|
-
never
|
|
162
|
-
] ? true : false;
|
|
163
|
-
/**
|
|
164
|
-
Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
|
|
165
|
-
|
|
166
|
-
@example
|
|
167
|
-
```
|
|
168
|
-
import type {Simplify} from 'type-fest';
|
|
169
|
-
|
|
170
|
-
type PositionProps = {
|
|
171
|
-
top: number;
|
|
172
|
-
left: number;
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
type SizeProps = {
|
|
176
|
-
width: number;
|
|
177
|
-
height: number;
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
// In your editor, hovering over `Props` will show a flattened object with all the properties.
|
|
181
|
-
type Props = Simplify<PositionProps & SizeProps>;
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
|
|
185
|
-
|
|
186
|
-
If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
|
|
187
|
-
|
|
188
|
-
@example
|
|
189
|
-
```
|
|
190
|
-
import type {Simplify} from 'type-fest';
|
|
191
|
-
|
|
192
|
-
interface SomeInterface {
|
|
193
|
-
foo: number;
|
|
194
|
-
bar?: string;
|
|
195
|
-
baz: number | undefined;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
type SomeType = {
|
|
199
|
-
foo: number;
|
|
200
|
-
bar?: string;
|
|
201
|
-
baz: number | undefined;
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
205
|
-
const someType: SomeType = literal;
|
|
206
|
-
const someInterface: SomeInterface = literal;
|
|
207
|
-
|
|
208
|
-
function fn(object: Record<string, unknown>): void {}
|
|
209
|
-
|
|
210
|
-
fn(literal); // Good: literal object type is sealed
|
|
211
|
-
fn(someType); // Good: type is sealed
|
|
212
|
-
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
213
|
-
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
217
|
-
@see SimplifyDeep
|
|
218
|
-
@category Object
|
|
219
|
-
*/
|
|
220
|
-
export type Simplify<T> = {
|
|
221
|
-
[KeyType in keyof T]: T[KeyType];
|
|
222
|
-
} & {};
|
|
223
|
-
/**
|
|
224
|
-
Returns the last element of a union type.
|
|
225
|
-
|
|
226
|
-
@example
|
|
227
|
-
```
|
|
228
|
-
type Last = LastOfUnion<1 | 2 | 3>;
|
|
229
|
-
//=> 3
|
|
230
|
-
```
|
|
231
|
-
*/
|
|
232
|
-
export type LastOfUnion<T> = UnionToIntersection<T extends any ? () => T : never> extends () => (infer R) ? R : never;
|
|
233
|
-
/**
|
|
234
|
-
Convert a union type into an unordered tuple type of its elements.
|
|
235
|
-
|
|
236
|
-
"Unordered" means the elements of the tuple are not guaranteed to be in the same order as in the union type. The arrangement can appear random and may change at any time.
|
|
237
|
-
|
|
238
|
-
This can be useful when you have objects with a finite set of keys and want a type defining only the allowed keys, but do not want to repeat yourself.
|
|
239
|
-
|
|
240
|
-
@example
|
|
241
|
-
```
|
|
242
|
-
import type {UnionToTuple} from 'type-fest';
|
|
243
|
-
|
|
244
|
-
type Numbers = 1 | 2 | 3;
|
|
245
|
-
type NumbersTuple = UnionToTuple<Numbers>;
|
|
246
|
-
//=> [1, 2, 3]
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
@example
|
|
250
|
-
```
|
|
251
|
-
import type {UnionToTuple} from 'type-fest';
|
|
252
|
-
|
|
253
|
-
const pets = {
|
|
254
|
-
dog: '🐶',
|
|
255
|
-
cat: '🐱',
|
|
256
|
-
snake: '🐍',
|
|
257
|
-
};
|
|
258
|
-
|
|
259
|
-
type Pet = keyof typeof pets;
|
|
260
|
-
//=> 'dog' | 'cat' | 'snake'
|
|
261
|
-
|
|
262
|
-
const petList = Object.keys(pets) as UnionToTuple<Pet>;
|
|
263
|
-
//=> ['dog', 'cat', 'snake']
|
|
264
|
-
```
|
|
265
|
-
|
|
266
|
-
@category Array
|
|
267
|
-
*/
|
|
268
|
-
export type UnionToTuple<T, L = LastOfUnion<T>> = IsNever<T> extends false ? [
|
|
269
|
-
...UnionToTuple<Exclude<T, L>>,
|
|
270
|
-
L
|
|
271
|
-
] : [
|
|
272
|
-
];
|
|
273
|
-
export type AutoInferExclude = "date-like" | "numerical";
|
|
274
|
-
export type GetKey<Value> = {
|
|
275
|
-
[Key in Exclude<ValueKey, AutoInferExclude>]: Value extends Values[Key] ? Key : never;
|
|
276
|
-
}[Exclude<ValueKey, AutoInferExclude>] extends infer SpecificKey ? SpecificKey extends never ? {
|
|
277
|
-
[Key in AutoInferExclude]: Value extends Values[Key] ? Key : never;
|
|
278
|
-
}[AutoInferExclude] : SpecificKey : never;
|
|
279
|
-
export type GetTypes<Value extends unknown[]> = Value extends [
|
|
280
|
-
infer Type
|
|
281
|
-
] ? Type : Value;
|
|
282
|
-
export type GetType<Value> = GetTypes<UnionToTuple<GetKey<Value>>>;
|
|
283
|
-
export type InferProperty<Value> = Value extends Property ? InferPropertyType<Value["type"]> : never;
|
|
284
|
-
export type InferPropertyType<Value> = Value extends (infer Type)[] ? InferPropertyTypeValue<Type>[] : InferPropertyTypeValue<Value>;
|
|
285
|
-
export type InferPropertyTypeValue<Value> = Value extends PropertyType ? Value extends Schema ? Inferred<Value> : Value extends Schematic<infer Model> ? Model : Value extends ValueKey ? Values[Value] : Value extends (infer NestedElementType)[] ? InferPropertyTypeValue<NestedElementType>[] : Value : never;
|
|
286
|
-
export type InferValue<Value> = Value extends ValueKey ? Values[Value] : Value extends ValueKey[] ? Values[Value[number]] : never;
|
|
287
|
-
export type Inferred<Model extends Schema> = Simplify<{
|
|
288
|
-
[Key in InferredRequiredProperties<Model>]: Model[Key] extends Property ? InferProperty<Model[Key]> : InferValue<Model[Key]>;
|
|
289
|
-
} & {
|
|
290
|
-
[Key in InferredOptionalProperties<Model>]?: Model[Key] extends Property ? InferProperty<Model[Key]> : never;
|
|
291
|
-
}>;
|
|
292
|
-
export type InferredOptionalProperties<Model extends Schema> = {
|
|
293
|
-
[Key in keyof Model]: Model[Key] extends Property ? Model[Key]["required"] extends false ? Key : never : never;
|
|
294
|
-
}[keyof Model];
|
|
295
|
-
export type InferredRequiredProperties<Model extends Schema> = {
|
|
296
|
-
[Key in keyof Model]: Model[Key] extends Property ? Model[Key]["required"] extends false ? never : Key : Key;
|
|
297
|
-
}[keyof Model];
|
|
298
|
-
export type OptionalProperty<Type> = {
|
|
299
|
-
required: false;
|
|
300
|
-
type: GetType<Type>;
|
|
301
|
-
};
|
|
302
|
-
export type Property = {
|
|
303
|
-
required?: boolean;
|
|
304
|
-
type: PropertyType | PropertyType[];
|
|
305
|
-
};
|
|
306
|
-
export type PropertyType = Schema | Schematic<unknown> | ValueKey;
|
|
307
|
-
export type RequiredProperty<Type> = {
|
|
308
|
-
required: true;
|
|
309
|
-
type: GetType<Type>;
|
|
310
|
-
};
|
|
311
|
-
/**
|
|
312
|
-
* A schema for validating objects
|
|
313
|
-
*/
|
|
314
|
-
export type Schema = {
|
|
315
|
-
[key: string]: Property | ValueKey | ValueKey[];
|
|
316
|
-
};
|
|
317
|
-
/**
|
|
318
|
-
* A schematic for validating objects
|
|
319
|
-
*/
|
|
320
|
-
export type Schematic<Model> = {
|
|
321
|
-
/**
|
|
322
|
-
* Does the value match the schema?
|
|
323
|
-
*/
|
|
324
|
-
is(value: unknown): value is Model;
|
|
325
|
-
};
|
|
326
|
-
export type Typed = Record<string, unknown>;
|
|
327
|
-
/**
|
|
328
|
-
* A typed schema for validating objects
|
|
329
|
-
*/
|
|
330
|
-
export type TypedSchema<Model extends Typed> = Simplify<{
|
|
331
|
-
[Key in RequiredKeysOf<Model>]: GetType<Model[Key]> | RequiredProperty<Model[Key]>;
|
|
332
|
-
} & {
|
|
333
|
-
[Key in OptionalKeysOf<Model>]: OptionalProperty<Model[Key]>;
|
|
334
|
-
}>;
|
|
335
|
-
export type ValueKey = keyof Values;
|
|
336
|
-
export type Values = {
|
|
337
|
-
array: unknown[];
|
|
338
|
-
bigint: bigint;
|
|
339
|
-
boolean: boolean;
|
|
340
|
-
date: Date;
|
|
341
|
-
"date-like": number | string | Date;
|
|
342
|
-
function: Function;
|
|
343
|
-
null: null;
|
|
344
|
-
number: number;
|
|
345
|
-
numerical: bigint | number;
|
|
346
|
-
object: object;
|
|
347
|
-
string: string;
|
|
348
|
-
symbol: symbol;
|
|
349
|
-
undefined: undefined;
|
|
350
|
-
};
|
|
351
|
-
/**
|
|
352
|
-
* Create a schematic from a schema
|
|
353
|
-
*/
|
|
354
|
-
export declare function schematic<Model extends Schema>(schema: Model): Schematic<Inferred<Model>>;
|
|
355
|
-
/**
|
|
356
|
-
* Create a schematic from a typed schema
|
|
357
|
-
*/
|
|
358
|
-
export declare function schematic<Model extends Typed>(schema: TypedSchema<Model>): Schematic<Model>;
|
|
359
|
-
|
|
360
|
-
export {};
|
package/types/is.d.cts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
// Generated by dts-bundle-generator v9.5.1
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A schematic for validating objects
|
|
5
|
-
*/
|
|
6
|
-
export type Schematic<Model> = {
|
|
7
|
-
/**
|
|
8
|
-
* Does the value match the schema?
|
|
9
|
-
*/
|
|
10
|
-
is(value: unknown): value is Model;
|
|
11
|
-
};
|
|
12
|
-
export declare function isDateLike(value: unknown): value is Date;
|
|
13
|
-
export declare function isSchematic(value: unknown): value is Schematic<never>;
|
|
14
|
-
|
|
15
|
-
export {};
|