@strictly/define 0.0.1 → 0.0.3
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/.out/index.d.ts +17 -11
- package/.out/index.js +17 -11
- package/.out/transformers/copies/copy.d.ts +3 -3
- package/.out/transformers/copies/copy_to.d.ts +3 -3
- package/.out/transformers/copies/copy_to.js +4 -0
- package/.out/transformers/copies/mobx_copy.d.ts +4 -4
- package/.out/transformers/copies/mobx_copy.js +1 -1
- package/.out/transformers/copies/specs/copy_to.tests.js +8 -8
- package/.out/transformers/copies/specs/mobx_copy.tests.js +1 -1
- package/.out/transformers/flatteners/flatten_accessors_of_type.d.ts +5 -0
- package/.out/transformers/flatteners/flatten_accessors_of_type.js +11 -0
- package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.d.ts +2 -2
- package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.js +2 -2
- package/.out/transformers/flatteners/{flatten_type_def_to.d.ts → flatten_type_to.d.ts} +1 -1
- package/.out/transformers/flatteners/{flatten_type_def_to.js → flatten_type_to.js} +1 -1
- package/.out/transformers/flatteners/{flatten_type_defs_of.d.ts → flatten_types_of_type.d.ts} +1 -1
- package/.out/transformers/flatteners/flatten_types_of_type.js +7 -0
- package/.out/transformers/flatteners/flatten_validation_errors_of_type.d.ts +22 -0
- package/.out/transformers/flatteners/flatten_validation_errors_of_type.js +10 -0
- package/.out/transformers/flatteners/flatten_validators_of_validating_type.d.ts +4 -0
- package/.out/transformers/flatteners/flatten_validators_of_validating_type.js +17 -0
- package/.out/transformers/flatteners/{flatten_value_type_to.d.ts → flatten_value_to.d.ts} +4 -4
- package/.out/transformers/flatteners/{flatten_value_type_to.js → flatten_value_to.js} +7 -2
- package/.out/transformers/flatteners/flatten_values_of_type.d.ts +3 -0
- package/.out/transformers/flatteners/flatten_values_of_type.js +7 -0
- package/.out/transformers/flatteners/specs/{flatten_accessors_of.tests.js → flatten_accessors_of_type.tests.js} +5 -6
- package/.out/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.js +6 -6
- package/.out/transformers/flatteners/specs/{flatten_type_def_to.tests.js → flatten_type_to.tests.js} +15 -13
- package/.out/transformers/flatteners/specs/flatten_types_of_type.tests.js +18 -0
- package/.out/transformers/flatteners/specs/flatten_validation_errors_of_type.tests.js +25 -0
- package/.out/transformers/flatteners/specs/{flatten_value_type_to.tests.js → flatten_value_to.tests.js} +81 -58
- package/.out/transformers/flatteners/specs/{flatten_value_types_of.tests.js → flatten_values_of_type.tests.js} +4 -4
- package/.out/transformers/flatteners/specs/value_path_to_type_path.tests.js +5 -3
- package/.out/transformers/flatteners/value_path_to_type_path.d.ts +1 -1
- package/.out/tsconfig.tsbuildinfo +1 -1
- package/.out/types/builders.d.ts +55 -29
- package/.out/types/builders.js +116 -32
- package/.out/types/definitions.d.ts +1 -1
- package/.out/types/flattened_accessors_of_type.d.ts +9 -0
- package/.out/types/{flattened_type_defs_of.d.ts → flattened_types_of_type.d.ts} +9 -6
- package/.out/types/flattened_types_of_validating_type.d.ts +25 -0
- package/.out/types/flattened_validators_of_validating_type.d.ts +10 -0
- package/.out/types/flattened_values_of_type.d.ts +6 -0
- package/.out/types/{mobx_value_type_of.d.ts → mobx_value_of_type.d.ts} +2 -2
- package/.out/types/{partial_type_def_of.d.ts → partial_type_of_type.d.ts} +1 -1
- package/.out/types/path_of.d.ts +1 -0
- package/.out/types/{json_paths_of.d.ts → paths_of_type.d.ts} +5 -5
- package/.out/types/readonly_type_of_type.d.ts +29 -0
- package/.out/types/specs/builder.tests.js +73 -23
- package/.out/types/specs/{flattened_accessors_of.tests.js → flattened_accessors_of_type.tests.js} +1 -1
- package/.out/types/specs/{flattened_type_defs_of.tests.js → flattened_types_of_type.tests.js} +22 -15
- package/.out/types/specs/flattened_validators_of_validating_type.tests.js +24 -0
- package/.out/types/specs/flattened_values_of_type.tests.js +18 -0
- package/.out/types/specs/{partial_type_def_of.tests.js → partial_type_of_type.tests.js} +4 -4
- package/.out/types/specs/{json_paths_of.tests.js → paths_of_type.tests.js} +19 -19
- package/.out/types/specs/{readonly_type_def_of.tests.js → readonly_type_of_type.tests.js} +6 -6
- package/.out/types/specs/type_of_type.tests.d.ts +1 -0
- package/.out/types/specs/type_of_type.tests.js +27 -0
- package/.out/types/specs/value_of_type.tests.d.ts +1 -0
- package/.out/types/specs/{value_type_of.tests.js → value_of_type.tests.js} +1 -1
- package/.out/types/specs/value_to_type_paths_of_type.tests.d.ts +1 -0
- package/.out/types/specs/{value_to_type_paths_of.tests.js → value_to_type_paths_of_type.tests.js} +9 -9
- package/.out/types/specs/value_types_of_discriminated_union.tests.js +2 -2
- package/.out/types/strict_definitions.d.ts +2 -2
- package/.out/types/type_of_type.d.ts +49 -0
- package/.out/types/type_of_type.js +70 -0
- package/.out/types/validating_definitions.d.ts +49 -0
- package/.out/types/validating_definitions.js +1 -0
- package/.out/types/validating_type_def_with_error.d.ts +55 -0
- package/.out/types/validating_type_def_with_error.js +1 -0
- package/.out/types/value_of_type.d.ts +24 -0
- package/.out/types/value_of_type.js +1 -0
- package/.out/types/{value_to_type_paths_of.d.ts → value_to_type_paths_of_type.d.ts} +13 -13
- package/.out/types/value_to_type_paths_of_type.js +1 -0
- package/.out/types/value_types_of_discriminated_union.d.ts +5 -4
- package/.out/validation/errors_of_validators.d.ts +4 -0
- package/.out/validation/errors_of_validators.js +1 -0
- package/.out/validation/specs/errors_of_validators.tests.d.ts +1 -0
- package/.out/validation/specs/errors_of_validators.tests.js +9 -0
- package/.out/validation/specs/validators_of_values.tests.d.ts +1 -0
- package/.out/validation/specs/validators_of_values.tests.js +21 -0
- package/.out/validation/validator.d.ts +21 -0
- package/.out/validation/validator.js +26 -0
- package/.out/validation/validators/defined_validator.d.ts +10 -0
- package/.out/validation/validators/defined_validator.js +18 -0
- package/.out/validation/validators/minimum_string_length_validator.d.ts +16 -0
- package/.out/validation/validators/minimum_string_length_validator.js +23 -0
- package/.out/validation/validators_of_values.d.ts +4 -0
- package/.out/validation/validators_of_values.js +1 -0
- package/.turbo/turbo-build.log +9 -9
- package/.turbo/turbo-check-types.log +1 -1
- package/.turbo/turbo-release$colon$exports.log +1 -1
- package/README.md +16 -2
- package/dist/index.cjs +364 -76
- package/dist/index.d.cts +365 -114
- package/dist/index.d.ts +365 -114
- package/dist/index.js +355 -73
- package/index.ts +17 -11
- package/package.json +11 -3
- package/transformers/copies/copy.ts +4 -4
- package/transformers/copies/copy_to.ts +11 -7
- package/transformers/copies/mobx_copy.ts +6 -6
- package/transformers/copies/specs/copy_to.tests.ts +8 -8
- package/transformers/copies/specs/mobx_copy.tests.ts +3 -3
- package/transformers/flatteners/{flatten_accessors_of.ts → flatten_accessors_of_type.ts} +10 -10
- package/transformers/flatteners/flatten_json_value_to_type_paths_of.ts +5 -5
- package/transformers/flatteners/{flatten_type_def_to.ts → flatten_type_to.ts} +1 -1
- package/transformers/flatteners/{flatten_type_defs_of.ts → flatten_types_of_type.ts} +3 -3
- package/transformers/flatteners/flatten_validation_errors_of_type.ts +93 -0
- package/transformers/flatteners/flatten_validators_of_validating_type.ts +32 -0
- package/transformers/flatteners/{flatten_value_type_to.ts → flatten_value_to.ts} +12 -7
- package/transformers/flatteners/{flatten_value_types_of.ts → flatten_values_of_type.ts} +6 -6
- package/transformers/flatteners/specs/{flatten_accessors_of.tests.ts → flatten_accessors_of_type.tests.ts} +10 -11
- package/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.ts +6 -6
- package/transformers/flatteners/specs/{flatten_type_def_to.tests.ts → flatten_type_to.tests.ts} +15 -13
- package/transformers/flatteners/specs/flatten_types_of_type.tests.ts +27 -0
- package/transformers/flatteners/specs/flatten_validation_errors_of_type.tests.ts +48 -0
- package/transformers/flatteners/specs/{flatten_value_type_to.tests.ts → flatten_value_to.tests.ts} +124 -91
- package/transformers/flatteners/specs/{flatten_value_types_of.tests.ts → flatten_values_of_type.tests.ts} +4 -4
- package/transformers/flatteners/specs/value_path_to_type_path.tests.ts +20 -18
- package/transformers/flatteners/value_path_to_type_path.ts +4 -4
- package/types/builders.ts +239 -71
- package/types/definitions.ts +2 -2
- package/types/{flattened_accessors_of.ts → flattened_accessors_of_type.ts} +3 -3
- package/types/{flattened_type_defs_of.ts → flattened_types_of_type.ts} +22 -12
- package/types/flattened_types_of_validating_type.ts +152 -0
- package/types/flattened_validators_of_validating_type.ts +33 -0
- package/types/flattened_values_of_type.ts +11 -0
- package/types/{mobx_value_type_of.ts → mobx_value_of_type.ts} +2 -2
- package/types/{partial_type_def_of.ts → partial_type_of_type.ts} +3 -1
- package/types/{json_path_of.ts → path_of.ts} +1 -1
- package/types/{json_paths_of.ts → paths_of_type.ts} +5 -5
- package/types/readonly_type_of_type.ts +54 -0
- package/types/specs/builder.tests.ts +205 -54
- package/types/specs/{flattened_accessors_of.tests.ts → flattened_accessors_of_type.tests.ts} +3 -3
- package/types/specs/{flattened_type_defs_of.tests.ts → flattened_types_of_type.tests.ts} +82 -34
- package/types/specs/flattened_validators_of_validating_type.tests.ts +47 -0
- package/types/specs/flattened_values_of_type.tests.ts +39 -0
- package/types/specs/{partial_type_def_of.tests.ts → partial_type_of_type.tests.ts} +11 -11
- package/types/specs/{json_paths_of.tests.ts → paths_of_type.tests.ts} +47 -47
- package/types/specs/{readonly_type_def_of.tests.ts → readonly_type_of_type.tests.ts} +14 -14
- package/types/specs/type_of_type.tests.ts +60 -0
- package/types/specs/{value_type_of.tests.ts → value_of_type.tests.ts} +15 -15
- package/types/specs/{value_to_type_paths_of.tests.ts → value_to_type_paths_of_type.tests.ts} +26 -25
- package/types/specs/value_types_of_discriminated_union.tests.ts +6 -6
- package/types/strict_definitions.ts +3 -3
- package/types/type_of_type.ts +178 -0
- package/types/validating_definitions.ts +109 -0
- package/types/validating_type_def_with_error.ts +93 -0
- package/types/value_of_type.ts +84 -0
- package/types/{value_to_type_paths_of.ts → value_to_type_paths_of_type.ts} +30 -29
- package/types/value_types_of_discriminated_union.ts +4 -3
- package/validation/errors_of_validators.ts +10 -0
- package/validation/specs/errors_of_validators.tests.ts +20 -0
- package/validation/specs/validators_of_values.tests.ts +68 -0
- package/validation/validator.ts +103 -0
- package/validation/validators/defined_validator.ts +22 -0
- package/validation/validators/minimum_string_length_validator.ts +36 -0
- package/validation/validators_of_values.ts +17 -0
- package/.out/transformers/flatteners/flatten_accessors_of.d.ts +0 -5
- package/.out/transformers/flatteners/flatten_accessors_of.js +0 -11
- package/.out/transformers/flatteners/flatten_type_defs_of.js +0 -7
- package/.out/transformers/flatteners/flatten_value_types_of.d.ts +0 -3
- package/.out/transformers/flatteners/flatten_value_types_of.js +0 -7
- package/.out/transformers/flatteners/specs/flatten_type_defs_of.tests.js +0 -17
- package/.out/types/flattened_accessors_of.d.ts +0 -9
- package/.out/types/flattened_value_types_of.d.ts +0 -6
- package/.out/types/json_path_of.d.ts +0 -1
- package/.out/types/readonly_type_def_of.d.ts +0 -29
- package/.out/types/specs/flattened_value_types_of.tests.js +0 -11
- package/.out/types/value_type_of.d.ts +0 -24
- package/transformers/flatteners/specs/flatten_type_defs_of.tests.ts +0 -26
- package/types/flattened_value_types_of.ts +0 -11
- package/types/readonly_type_def_of.ts +0 -53
- package/types/specs/flattened_value_types_of.tests.ts +0 -21
- package/types/value_type_of.ts +0 -84
- /package/.out/transformers/flatteners/specs/{flatten_accessors_of.tests.d.ts → flatten_accessors_of_type.tests.d.ts} +0 -0
- /package/.out/transformers/flatteners/specs/{flatten_type_def_to.tests.d.ts → flatten_type_to.tests.d.ts} +0 -0
- /package/.out/transformers/flatteners/specs/{flatten_type_defs_of.tests.d.ts → flatten_types_of_type.tests.d.ts} +0 -0
- /package/.out/transformers/flatteners/specs/{flatten_value_type_to.tests.d.ts → flatten_validation_errors_of_type.tests.d.ts} +0 -0
- /package/.out/transformers/flatteners/specs/{flatten_value_types_of.tests.d.ts → flatten_value_to.tests.d.ts} +0 -0
- /package/.out/{types/flattened_accessors_of.js → transformers/flatteners/specs/flatten_values_of_type.tests.d.ts} +0 -0
- /package/.out/types/{flattened_type_defs_of.js → flattened_accessors_of_type.js} +0 -0
- /package/.out/types/{flattened_value_types_of.js → flattened_types_of_type.js} +0 -0
- /package/.out/types/{json_path_of.js → flattened_types_of_validating_type.js} +0 -0
- /package/.out/types/{json_paths_of.js → flattened_validators_of_validating_type.js} +0 -0
- /package/.out/types/{partial_type_def_of.js → flattened_values_of_type.js} +0 -0
- /package/.out/types/{mobx_value_type_of.js → mobx_value_of_type.js} +0 -0
- /package/.out/types/{readonly_type_def_of.js → partial_type_of_type.js} +0 -0
- /package/.out/types/{specs/flattened_accessors_of.tests.d.ts → path_of.js} +0 -0
- /package/.out/types/{specs/flattened_type_defs_of.tests.d.ts → paths_of_type.js} +0 -0
- /package/.out/types/{specs/flattened_value_types_of.tests.d.ts → readonly_type_of_type.js} +0 -0
- /package/.out/types/specs/{json_paths_of.tests.d.ts → flattened_accessors_of_type.tests.d.ts} +0 -0
- /package/.out/types/specs/{partial_type_def_of.tests.d.ts → flattened_types_of_type.tests.d.ts} +0 -0
- /package/.out/types/specs/{readonly_type_def_of.tests.d.ts → flattened_validators_of_validating_type.tests.d.ts} +0 -0
- /package/.out/types/specs/{value_to_type_paths_of.tests.d.ts → flattened_values_of_type.tests.d.ts} +0 -0
- /package/.out/types/specs/{value_type_of.tests.d.ts → partial_type_of_type.tests.d.ts} +0 -0
- /package/.out/types/{value_to_type_paths_of.js → specs/paths_of_type.tests.d.ts} +0 -0
- /package/.out/types/{value_type_of.js → specs/readonly_type_of_type.tests.d.ts} +0 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { map, UnreachableError, } from '@strictly/base';
|
|
2
|
+
import { TypeDefType, } from 'types/definitions';
|
|
3
|
+
export function typeOfType({ definition }) {
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
5
|
+
return {
|
|
6
|
+
definition: typeDefOfTypeDef(definition),
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
function typeDefOfTypeDef(t) {
|
|
10
|
+
switch (t.type) {
|
|
11
|
+
case TypeDefType.Literal:
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
13
|
+
return typeDefOfLiteralTypeDef(t);
|
|
14
|
+
case TypeDefType.List:
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
16
|
+
return typeDefOfListTypeDef(t);
|
|
17
|
+
case TypeDefType.Record:
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
19
|
+
return typeDefOfRecordTypeDef(t);
|
|
20
|
+
case TypeDefType.Object:
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
22
|
+
return typeDefOfObjectTypeDef(t);
|
|
23
|
+
case TypeDefType.Union:
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
25
|
+
return typeDefOfUnionTypeDef(t);
|
|
26
|
+
default:
|
|
27
|
+
throw new UnreachableError(t);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function typeDefOfLiteralTypeDef({ type, valuePrototype, }) {
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
32
|
+
return {
|
|
33
|
+
type,
|
|
34
|
+
valuePrototype,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function typeDefOfListTypeDef({ type, elements, }) {
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
39
|
+
return {
|
|
40
|
+
type,
|
|
41
|
+
elements: typeDefOfTypeDef(elements),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function typeDefOfRecordTypeDef({ type, keyPrototype, valueTypeDef, }) {
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
46
|
+
return {
|
|
47
|
+
type,
|
|
48
|
+
keyPrototype,
|
|
49
|
+
valueTypeDef: typeDefOfTypeDef(valueTypeDef),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function typeDefOfObjectTypeDef({ type, fields, }) {
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
54
|
+
return {
|
|
55
|
+
type,
|
|
56
|
+
fields: map(fields, function (_k, v) {
|
|
57
|
+
return typeDefOfTypeDef(v);
|
|
58
|
+
}),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function typeDefOfUnionTypeDef({ type, discriminator, unions, }) {
|
|
62
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
63
|
+
return {
|
|
64
|
+
type,
|
|
65
|
+
discriminator,
|
|
66
|
+
unions: map(unions, function (_k, v) {
|
|
67
|
+
return typeDefOfTypeDef(v);
|
|
68
|
+
}),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type ObjectFieldKey, type RecordKeyType, type TypeDefType, type UnionKey } from './definitions';
|
|
2
|
+
export type ValidatingType<T extends ValidatingTypeDef = ValidatingTypeDef> = {
|
|
3
|
+
readonly definition: T;
|
|
4
|
+
};
|
|
5
|
+
export type Rule<E = any, V = any> = (v: V) => E | null;
|
|
6
|
+
export type ErrorOfValidatingTypeDef<T extends ValidatingTypeDef> = T extends ValidatingTypeDef<infer E> ? E : never;
|
|
7
|
+
export type ValidatingTypeDef<E = any> = ValidatingLiteralTypeDef<E> | ValidatingListTypeDef<E> | ValidatingRecordTypeDef<E> | ValidatingObjectTypeDef<E> | ValidatingUnionTypeDef<E>;
|
|
8
|
+
type AnyTypeDef = any;
|
|
9
|
+
export type ValidatingLiteralTypeDef<E = any, V = any> = {
|
|
10
|
+
readonly type: TypeDefType.Literal;
|
|
11
|
+
readonly valuePrototype: [V];
|
|
12
|
+
readonly rule: Rule<E>;
|
|
13
|
+
readonly required: boolean;
|
|
14
|
+
readonly readonly: boolean;
|
|
15
|
+
};
|
|
16
|
+
export type ValidatingListTypeDef<E = any, Ele extends ValidatingTypeDef = AnyTypeDef> = {
|
|
17
|
+
readonly type: TypeDefType.List;
|
|
18
|
+
readonly elements: Ele;
|
|
19
|
+
readonly rule: Rule<E>;
|
|
20
|
+
readonly required: boolean;
|
|
21
|
+
readonly readonly: boolean;
|
|
22
|
+
};
|
|
23
|
+
export type ValidatingRecordTypeDef<E = any, K extends RecordKeyType = RecordKeyType, V extends ValidatingTypeDef | undefined = AnyTypeDef> = {
|
|
24
|
+
readonly type: TypeDefType.Record;
|
|
25
|
+
readonly keyPrototype: K;
|
|
26
|
+
readonly valueTypeDef: V;
|
|
27
|
+
readonly rule: Rule<E>;
|
|
28
|
+
readonly required: boolean;
|
|
29
|
+
readonly readonly: boolean;
|
|
30
|
+
};
|
|
31
|
+
export type ValidatingObjectTypeDefFields = {
|
|
32
|
+
[Key: ObjectFieldKey]: AnyTypeDef;
|
|
33
|
+
};
|
|
34
|
+
export type ValidatingObjectTypeDef<E = any, Fields extends ValidatingObjectTypeDefFields = ValidatingObjectTypeDefFields> = {
|
|
35
|
+
readonly type: TypeDefType.Object;
|
|
36
|
+
readonly fields: Fields;
|
|
37
|
+
readonly rule: Rule<E>;
|
|
38
|
+
readonly required: boolean;
|
|
39
|
+
readonly readonly: boolean;
|
|
40
|
+
};
|
|
41
|
+
export type ValidatingUnionTypeDef<E = any, D extends string | null = string | null, U extends Readonly<Record<UnionKey, AnyTypeDef>> = Readonly<Record<UnionKey, AnyTypeDef>>> = {
|
|
42
|
+
readonly discriminator: D;
|
|
43
|
+
readonly type: TypeDefType.Union;
|
|
44
|
+
readonly unions: U;
|
|
45
|
+
readonly rule: Rule<E>;
|
|
46
|
+
readonly required: boolean;
|
|
47
|
+
readonly readonly: boolean;
|
|
48
|
+
};
|
|
49
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type IsFieldReadonly } from '@strictly/base';
|
|
2
|
+
import { type TypeDefType } from 'types/definitions';
|
|
3
|
+
import { type Rule, type ValidatingListTypeDef, type ValidatingLiteralTypeDef, type ValidatingObjectTypeDef, type ValidatingRecordTypeDef, type ValidatingTypeDef, type ValidatingUnionTypeDef } from './validating_definitions';
|
|
4
|
+
export type ValidatingTypeDefWithError<T extends ValidatingTypeDef, E> = T extends ValidatingLiteralTypeDef ? ValidatingLiteralTypeDefWithError<T, E> : T extends ValidatingListTypeDef ? ValidatingListTypeDefWithError<T, E> : T extends ValidatingRecordTypeDef ? ValidatingRecordTypeDefWithError<T, E> : T extends ValidatingObjectTypeDef ? ValidatingObjectTypeDefWithError<T, E> : T extends ValidatingUnionTypeDef ? ValidatingUnionTypeDefWithError<T, E> : never;
|
|
5
|
+
type ValidatingLiteralTypeDefWithError<T extends ValidatingLiteralTypeDef, E2> = T extends ValidatingLiteralTypeDef<infer E1, infer V> ? {
|
|
6
|
+
readonly type: TypeDefType.Literal;
|
|
7
|
+
readonly valuePrototype: [V];
|
|
8
|
+
readonly rule: Rule<E1 | E2>;
|
|
9
|
+
readonly required: boolean;
|
|
10
|
+
readonly readonly: boolean;
|
|
11
|
+
} : never;
|
|
12
|
+
type ValidatingListTypeDefWithError<T extends ValidatingListTypeDef, E2> = T extends ValidatingListTypeDef<infer E1, infer E> ? IsFieldReadonly<T, 'elements'> extends true ? {
|
|
13
|
+
readonly type: TypeDefType.List;
|
|
14
|
+
readonly elements: E;
|
|
15
|
+
readonly rule: Rule<E1 | E2>;
|
|
16
|
+
readonly required: boolean;
|
|
17
|
+
readonly readonly: boolean;
|
|
18
|
+
} : {
|
|
19
|
+
readonly type: TypeDefType.List;
|
|
20
|
+
elements: E;
|
|
21
|
+
readonly rule: Rule<E1 | E2>;
|
|
22
|
+
readonly required: boolean;
|
|
23
|
+
readonly readonly: boolean;
|
|
24
|
+
} : never;
|
|
25
|
+
type ValidatingRecordTypeDefWithError<T extends ValidatingRecordTypeDef, E2> = T extends ValidatingRecordTypeDef<infer E1, infer K, infer V> ? IsFieldReadonly<T, 'valueTypeDef'> extends true ? {
|
|
26
|
+
readonly type: TypeDefType.Record;
|
|
27
|
+
readonly keyPrototype: K;
|
|
28
|
+
readonly valueTypeDef: V;
|
|
29
|
+
readonly rule: Rule<E1 | E2>;
|
|
30
|
+
readonly required: boolean;
|
|
31
|
+
readonly readonly: boolean;
|
|
32
|
+
} : {
|
|
33
|
+
readonly type: TypeDefType.Record;
|
|
34
|
+
readonly keyPrototype: K;
|
|
35
|
+
valueTypeDef: V;
|
|
36
|
+
readonly rule: Rule<E1 | E2>;
|
|
37
|
+
readonly required: boolean;
|
|
38
|
+
readonly readonly: boolean;
|
|
39
|
+
} : never;
|
|
40
|
+
type ValidatingObjectTypeDefWithError<T extends ValidatingObjectTypeDef, E2> = T extends ValidatingObjectTypeDef<infer E1, infer Fields> ? {
|
|
41
|
+
readonly type: TypeDefType.Object;
|
|
42
|
+
readonly fields: Fields;
|
|
43
|
+
readonly rule: Rule<E1 | E2>;
|
|
44
|
+
readonly required: boolean;
|
|
45
|
+
readonly readonly: boolean;
|
|
46
|
+
} : never;
|
|
47
|
+
type ValidatingUnionTypeDefWithError<T extends ValidatingUnionTypeDef, E2> = T extends ValidatingUnionTypeDef<infer E1, infer D, infer U> ? {
|
|
48
|
+
readonly type: TypeDefType.Union;
|
|
49
|
+
readonly discriminator: D;
|
|
50
|
+
readonly unions: U;
|
|
51
|
+
readonly rule: Rule<E1 | E2>;
|
|
52
|
+
readonly required: boolean;
|
|
53
|
+
readonly readonly: boolean;
|
|
54
|
+
} : never;
|
|
55
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type IsFieldReadonly } from '@strictly/base';
|
|
2
|
+
import { type ListTypeDef, type LiteralTypeDef, type ObjectTypeDef, type RecordTypeDef, type Type, type TypeDef, type UnionTypeDef } from './definitions';
|
|
3
|
+
export type ValueOfType<T, Extra = {}> = T extends Type ? ValueOfTypeDef<T['definition'], Extra> : never;
|
|
4
|
+
export type ValueOfTypeDef<F extends TypeDef, Extra = {}> = F extends LiteralTypeDef ? ValueOfLiteralTypeDef<F> : F extends ListTypeDef ? ValueOfListTypeDef<F, Extra> : F extends RecordTypeDef ? ValueOfRecordTypeDef<F, Extra> : F extends ObjectTypeDef ? ValueOfObjectTypeDef<F, Extra> : F extends UnionTypeDef ? ValueOfUnionTypeDef<F, Extra> : never;
|
|
5
|
+
type ValueOfLiteralTypeDef<F extends LiteralTypeDef> = F['valuePrototype'][number];
|
|
6
|
+
type ValueOfListTypeDef<F extends ListTypeDef, Extra> = IsFieldReadonly<F, 'elements'> extends true ? readonly ValueOfTypeDef<F['elements'], Extra>[] & Extra : ValueOfTypeDef<F['elements'], Extra>[] & Extra;
|
|
7
|
+
type ValueOfRecordTypeDef<F extends RecordTypeDef, Extra> = undefined extends F['valueTypeDef'] ? IsFieldReadonly<F, 'valueTypeDef'> extends true ? {
|
|
8
|
+
readonly [k in F['keyPrototype']]?: ValueOfTypeDef<F['valueTypeDef'], Extra>;
|
|
9
|
+
} : {
|
|
10
|
+
[k in F['keyPrototype']]?: ValueOfTypeDef<F['valueTypeDef'], Extra>;
|
|
11
|
+
} : IsFieldReadonly<F, 'valueTypeDef'> extends true ? {
|
|
12
|
+
readonly [k in F['keyPrototype']]: ValueOfTypeDef<F['valueTypeDef'], Extra>;
|
|
13
|
+
} : {
|
|
14
|
+
[k in F['keyPrototype']]: ValueOfTypeDef<F['valueTypeDef'], Extra>;
|
|
15
|
+
};
|
|
16
|
+
type ValueOfObjectTypeDef<F extends ObjectTypeDef, Extra> = F extends ObjectTypeDef<infer Fields> ? {
|
|
17
|
+
[K in keyof Fields]: ValueOfTypeDef<Fields[K], Extra>;
|
|
18
|
+
} & Extra : never;
|
|
19
|
+
type ValueOfUnionTypeDef<F extends UnionTypeDef, Extra> = F extends UnionTypeDef<infer D, infer U> ? D extends string ? {
|
|
20
|
+
[K in keyof U]: ValueOfTypeDef<U[K], Extra> & Readonly<Record<D, K>>;
|
|
21
|
+
}[keyof U] : {
|
|
22
|
+
[K in keyof U]: ValueOfTypeDef<U[K], Extra>;
|
|
23
|
+
}[keyof U] : never;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { type SimplifyDeep, type UnionToIntersection } from 'type-fest';
|
|
2
2
|
import { type ListTypeDef, type LiteralTypeDef, type ObjectTypeDef, type RecordTypeDef, type TypeDef, type UnionTypeDef } from './definitions';
|
|
3
3
|
import { type Depths, type StartingDepth } from './flattened';
|
|
4
|
-
import { type
|
|
4
|
+
import { type PathOf } from './path_of';
|
|
5
5
|
import { type StrictType } from './strict_definitions';
|
|
6
|
-
export type
|
|
7
|
-
type
|
|
6
|
+
export type ValueToTypePathsOfType<T extends StrictType, SegmentOverride extends string = '*', Path extends string = '$'> = SimplifyDeep<InternalFlattenedTypePathsOf<T['definition'], SegmentOverride, Path, Path, StartingDepth>>;
|
|
7
|
+
type InternalFlattenedTypePathsOf<T extends TypeDef, SegmentOverride extends string, ValuePath extends string, TypePath extends string, Depth extends number> = {
|
|
8
8
|
readonly [K in ValuePath]: TypePath;
|
|
9
|
-
} &
|
|
10
|
-
type
|
|
11
|
-
type
|
|
12
|
-
type
|
|
13
|
-
type
|
|
14
|
-
type
|
|
15
|
-
readonly [K in keyof Fields]-?:
|
|
9
|
+
} & InternalFlattenedTypePathsOfChildren<T, SegmentOverride, ValuePath, TypePath, '', Depth>;
|
|
10
|
+
type InternalFlattenedTypePathsOfChildren<T extends TypeDef, SegmentOverride extends string, ValuePath extends string, TypePath extends string, Qualifier extends string, Depth extends number, NextDepth extends number = Depths[Depth]> = NextDepth extends -1 ? never : T extends LiteralTypeDef ? InternalFlattenedTypePathsOfLiteralChildren : T extends ListTypeDef ? InternalFlattenedTypePathsOfListChildren<T, SegmentOverride, ValuePath, TypePath, NextDepth> : T extends RecordTypeDef ? InternalFlattenedTypePathsOfRecordChildren<T, SegmentOverride, ValuePath, TypePath, NextDepth> : T extends ObjectTypeDef ? InternalFlattenedTypePathsOfObjectChildren<T, SegmentOverride, ValuePath, TypePath, Qualifier, NextDepth> : T extends UnionTypeDef ? InternalFlattenedTypePathsOfUnionChildren<T, SegmentOverride, ValuePath, TypePath, Qualifier, NextDepth> : never;
|
|
11
|
+
type InternalFlattenedTypePathsOfLiteralChildren = {};
|
|
12
|
+
type InternalFlattenedTypePathsOfListChildren<T extends ListTypeDef, SegmentOverride extends string, ValuePath extends string, TypePath extends string, Depth extends number> = InternalFlattenedTypePathsOf<T['elements'], SegmentOverride, PathOf<ValuePath, number>, PathOf<TypePath, number, SegmentOverride>, Depth>;
|
|
13
|
+
type InternalFlattenedTypePathsOfRecordChildren<T extends RecordTypeDef, SegmentOverride extends string, ValuePath extends string, TypePath extends string, Depth extends number> = InternalFlattenedTypePathsOf<T['valueTypeDef'], SegmentOverride, PathOf<ValuePath, T['keyPrototype']>, PathOf<TypePath, T['keyPrototype'], SegmentOverride>, Depth>;
|
|
14
|
+
type InternalFlattenedTypePathsOfObjectChildren<T extends ObjectTypeDef, SegmentOverride extends string, ValuePath extends string, TypePath extends string, Qualifier extends string, Depth extends number> = T extends ObjectTypeDef<infer Fields> ? keyof Fields extends string ? UnionToIntersection<{
|
|
15
|
+
readonly [K in keyof Fields]-?: InternalFlattenedTypePathsOf<Exclude<Fields[K], undefined>, SegmentOverride, PathOf<ValuePath, `${Qualifier}${K}`>, PathOf<TypePath, `${Qualifier}${K}`>, Depth>;
|
|
16
16
|
}[keyof Fields]> : never : never;
|
|
17
|
-
type
|
|
18
|
-
readonly [K in keyof Unions]:
|
|
17
|
+
type InternalFlattenedTypePathsOfUnionChildren<T extends UnionTypeDef, SegmentOverride extends string, ValuePath extends string, TypePath extends string, Qualifier extends string, Depth extends number> = T extends UnionTypeDef<infer D, infer Unions> ? keyof Unions extends string ? D extends null ? UnionToIntersection<{
|
|
18
|
+
readonly [K in keyof Unions]: InternalFlattenedTypePathsOfChildren<Unions[K], SegmentOverride, ValuePath, TypePath, '', Depth>;
|
|
19
19
|
}[keyof Unions]> : UnionToIntersection<{
|
|
20
|
-
readonly [K in keyof Unions]:
|
|
20
|
+
readonly [K in keyof Unions]: InternalFlattenedTypePathsOfChildren<Unions[K], SegmentOverride, ValuePath, TypePath, `${Qualifier}${K}:`, Depth>;
|
|
21
21
|
}[keyof Unions]> : never : never;
|
|
22
22
|
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { type UnionTypeDef } from './definitions';
|
|
2
|
-
import { type
|
|
2
|
+
import { type ReadonlyTypeOfType } from './readonly_type_of_type';
|
|
3
|
+
import { type ValueOfType } from './value_of_type';
|
|
3
4
|
export type ValueTypesOfDiscriminatedUnion<U extends UnionTypeDef> = U extends UnionTypeDef<infer D, infer Unions> ? D extends string ? {
|
|
4
|
-
[K in keyof Unions]:
|
|
5
|
+
[K in keyof Unions]: ValueOfType<ReadonlyTypeOfType<{
|
|
5
6
|
definition: Unions[K];
|
|
6
|
-
}
|
|
7
|
-
[KD in D]: K;
|
|
7
|
+
}>> & {
|
|
8
|
+
readonly [KD in D]: K;
|
|
8
9
|
};
|
|
9
10
|
} : never : never;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
describe('FlattenedValidatorsOfType', function () {
|
|
2
|
+
describe('literal', function () {
|
|
3
|
+
let t;
|
|
4
|
+
it('has the expected type', function () {
|
|
5
|
+
expectTypeOf().toEqualTypeOf(t);
|
|
6
|
+
});
|
|
7
|
+
});
|
|
8
|
+
describe('list', function () {
|
|
9
|
+
let t;
|
|
10
|
+
it('has the expected type', function () {
|
|
11
|
+
expectTypeOf().toEqualTypeOf(t);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
describe('with defaults', function () {
|
|
15
|
+
let t;
|
|
16
|
+
it('has the expected type', function () {
|
|
17
|
+
expectTypeOf().toEqualTypeOf(t);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Simplify } from 'type-fest';
|
|
2
|
+
export type FunctionalValidator<V = any, E = any, ValuePath extends string = any, Context = any> = (v: V, valuePath: ValuePath, context: Context) => E | null;
|
|
3
|
+
export type AnnotatedValidator<V = any, E = any, ValuePath extends string = any, Context = any> = {
|
|
4
|
+
readonly validate: (v: V, valuePath: ValuePath, context: Context) => E | null;
|
|
5
|
+
readonly annotations: (valuePath: ValuePath, context: Context) => {
|
|
6
|
+
readonly required: boolean;
|
|
7
|
+
readonly readonly: boolean;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export type Validator<V = any, E = any, ValuePath extends string = any, Context = any> = FunctionalValidator<V, E, ValuePath, Context> | AnnotatedValidator<V, E, ValuePath, Context>;
|
|
11
|
+
export type ErrorOfValidator<V extends Validator> = V extends Validator<infer _V, infer E> ? E : never;
|
|
12
|
+
export type ValidationError<Type extends string, Data = {}> = Simplify<{
|
|
13
|
+
type: Type;
|
|
14
|
+
} & Data>;
|
|
15
|
+
export declare function isFunctionalValidator(v: Validator): v is FunctionalValidator;
|
|
16
|
+
export declare function isAnnotatedValidator(v: Validator): v is AnnotatedValidator;
|
|
17
|
+
export declare function validate<V = any, E = any, ValuePath extends string = any, Context = any>(validator: Validator<V, E, ValuePath, Context>, v: V, valuePath: ValuePath, context: Context): E | null;
|
|
18
|
+
export declare function annotations<V = any, E = any, ValuePath extends string = any, Context = any>(validator: Validator<V, E, ValuePath, Context>, valuePath: ValuePath, context: Context): {
|
|
19
|
+
readonly required: boolean;
|
|
20
|
+
readonly readonly: boolean;
|
|
21
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export function isFunctionalValidator(v) {
|
|
2
|
+
return typeof v === 'function';
|
|
3
|
+
}
|
|
4
|
+
export function isAnnotatedValidator(v) {
|
|
5
|
+
return typeof v !== 'function';
|
|
6
|
+
}
|
|
7
|
+
export function validate(validator, v, valuePath, context) {
|
|
8
|
+
if (isAnnotatedValidator(validator)) {
|
|
9
|
+
return validator.validate(v, valuePath, context);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
13
|
+
return validator(v, valuePath, context);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function annotations(validator, valuePath, context) {
|
|
17
|
+
if (isAnnotatedValidator(validator)) {
|
|
18
|
+
return validator.annotations(valuePath, context);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
return {
|
|
22
|
+
required: false,
|
|
23
|
+
readonly: false,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type AnnotatedValidator } from 'validation/validator';
|
|
2
|
+
export declare class DefinedValidator<V, E, ValuePath extends string, Context> implements AnnotatedValidator<V | null | undefined, E, ValuePath, Context> {
|
|
3
|
+
private readonly error;
|
|
4
|
+
constructor(error: E);
|
|
5
|
+
validate(v: V | null | undefined): E | null;
|
|
6
|
+
annotations(): {
|
|
7
|
+
required: boolean;
|
|
8
|
+
readonly: boolean;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export class DefinedValidator {
|
|
2
|
+
error;
|
|
3
|
+
constructor(error) {
|
|
4
|
+
this.error = error;
|
|
5
|
+
}
|
|
6
|
+
validate(v) {
|
|
7
|
+
if (v == null) {
|
|
8
|
+
return this.error;
|
|
9
|
+
}
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
annotations() {
|
|
13
|
+
return {
|
|
14
|
+
required: true,
|
|
15
|
+
readonly: false,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type AnnotatedValidator } from 'validation/validator';
|
|
2
|
+
export declare const MinimumStringLengthValidationErrorType = "minimum_string_length";
|
|
3
|
+
export type MinimumStringLengthValidationError = {
|
|
4
|
+
type: typeof MinimumStringLengthValidationErrorType;
|
|
5
|
+
receivedLength: number;
|
|
6
|
+
minimumLength: number;
|
|
7
|
+
};
|
|
8
|
+
export declare class MinimumStringLengthValidator<ValuePath extends string, Context> implements AnnotatedValidator<string, MinimumStringLengthValidationError, ValuePath, Context> {
|
|
9
|
+
private readonly minimumLength;
|
|
10
|
+
constructor(minimumLength: number);
|
|
11
|
+
validate(value: string): MinimumStringLengthValidationError | null;
|
|
12
|
+
annotations(): {
|
|
13
|
+
required: boolean;
|
|
14
|
+
readonly: boolean;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const MinimumStringLengthValidationErrorType = 'minimum_string_length';
|
|
2
|
+
export class MinimumStringLengthValidator {
|
|
3
|
+
minimumLength;
|
|
4
|
+
constructor(minimumLength) {
|
|
5
|
+
this.minimumLength = minimumLength;
|
|
6
|
+
}
|
|
7
|
+
validate(value) {
|
|
8
|
+
if (value.length < this.minimumLength) {
|
|
9
|
+
return {
|
|
10
|
+
type: MinimumStringLengthValidationErrorType,
|
|
11
|
+
minimumLength: this.minimumLength,
|
|
12
|
+
receivedLength: value.length,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
annotations() {
|
|
18
|
+
return {
|
|
19
|
+
required: true,
|
|
20
|
+
readonly: false,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type Validator } from 'validation/validator';
|
|
2
|
+
export type ValidatorsOfValues<FlattenedValues extends Readonly<Record<string, any>>, TypePathsToValuePaths extends Readonly<Record<keyof FlattenedValues, string>> = Readonly<Record<keyof FlattenedValues, any>>, Context = any> = {
|
|
3
|
+
readonly [K in keyof FlattenedValues]: Validator<FlattenedValues[K], any, TypePathsToValuePaths[K], Context>;
|
|
4
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/.turbo/turbo-build.log
CHANGED
|
@@ -3,16 +3,16 @@ $ tsup
|
|
|
3
3
|
[34mCLI[39m Building entry: index.ts
|
|
4
4
|
[34mCLI[39m Using tsconfig: tsconfig.build.json
|
|
5
5
|
[34mCLI[39m tsup v8.3.5
|
|
6
|
-
[34mCLI[39m Using tsup config: /home/runner/work/
|
|
6
|
+
[34mCLI[39m Using tsup config: /home/runner/work/strictly/strictly/packages/define/tsup.config.ts
|
|
7
7
|
[34mCLI[39m Target: esnext
|
|
8
8
|
[34mCJS[39m Build start
|
|
9
9
|
[34mESM[39m Build start
|
|
10
|
-
[32mCJS[39m [1mdist/index.cjs [22m[
|
|
11
|
-
[32mCJS[39m ⚡️ Build success in
|
|
12
|
-
[32mESM[39m [1mdist/index.js [22m[
|
|
13
|
-
[32mESM[39m ⚡️ Build success in
|
|
10
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m26.97 KB[39m
|
|
11
|
+
[32mCJS[39m ⚡️ Build success in 63ms
|
|
12
|
+
[32mESM[39m [1mdist/index.js [22m[32m24.77 KB[39m
|
|
13
|
+
[32mESM[39m ⚡️ Build success in 69ms
|
|
14
14
|
[34mDTS[39m Build start
|
|
15
|
-
[32mDTS[39m ⚡️ Build success in
|
|
16
|
-
[32mDTS[39m [1mdist/index.d.cts [22m[
|
|
17
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[
|
|
18
|
-
Done in
|
|
15
|
+
[32mDTS[39m ⚡️ Build success in 5479ms
|
|
16
|
+
[32mDTS[39m [1mdist/index.d.cts [22m[32m38.05 KB[39m
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m38.05 KB[39m
|
|
18
|
+
Done in 6.32s.
|
package/README.md
CHANGED
|
@@ -1,12 +1,26 @@
|
|
|
1
|
-
# Define
|
|
2
|
-
|
|
3
1
|
Type definitions for specifying and modifying types
|
|
4
2
|
|
|
3
|
+
# Define Your Types
|
|
4
|
+
|
|
5
|
+
One of the goals of @strictly/form is to provide a type-safe mapping between your display types and your domain types. In order to achieve this we need to be able to specify those types.
|
|
6
|
+
|
|
5
7
|
## Defining Types
|
|
6
8
|
|
|
7
9
|
## Type Builder
|
|
8
10
|
|
|
11
|
+
# Transforming Types
|
|
12
|
+
|
|
9
13
|
## Modifying Types
|
|
10
14
|
|
|
11
15
|
## Modifying Values
|
|
12
16
|
|
|
17
|
+
# FAQ
|
|
18
|
+
|
|
19
|
+
## Why not use Zod (or equivalent)?
|
|
20
|
+
|
|
21
|
+
While [Zod](https://zod.dev/) is a great tool, with many, many features and great supporting libraries, it does not produce 100% introspectable type definitions. Our definitions need to be quite minimal, while Zod's are quite fully featured. The reasons for this are
|
|
22
|
+
1. The Typescript compiler is actually fairly limited in how much it processing it will do on a [Mapped Type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html) before it throws the dreaded `Type instantiation is excessively deep and possibly infinite. ts(2589)` [error](https://github.com/microsoft/TypeScript/issues/30188#issuecomment-469285102). The types we use are tuned to circumvent this restriction as much as possible.
|
|
23
|
+
2. @strictly transforms the type definitions in multiple ways. The more variation allowed in the base types, the more cases the transformers need to deal with (and the more processing we need to do on those mapped types). Essentially the amount of code you have to write is quadratic (the number of supported type definitions multiplied by the number of transformations). As the number of transformations is potentially unbounded, keeping the number of distinct type definitions to a minimum is key
|
|
24
|
+
|
|
25
|
+
It's a definite possibility that someone could write an adapter from Zod to our internal type definitions, but it's not practical to use Zod in place of the internal type definitions.
|
|
26
|
+
|