@strictly/define 0.0.26 → 0.0.28
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/transformers/flatteners/flatten_accessors_of_type.d.ts +1 -1
- package/.out/transformers/flatteners/flatten_accessors_of_type.js +2 -2
- package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.d.ts +1 -1
- package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.js +5 -2
- package/.out/transformers/flatteners/flatten_validation_errors_of_type.d.ts +1 -1
- package/.out/transformers/flatteners/flatten_validation_errors_of_type.js +2 -2
- package/.out/transformers/flatteners/flatten_value_to.d.ts +1 -1
- package/.out/transformers/flatteners/flatten_value_to.js +35 -20
- package/.out/transformers/flatteners/flatten_values_of_type.d.ts +1 -1
- package/.out/transformers/flatteners/flatten_values_of_type.js +2 -2
- package/.out/tsconfig.tsbuildinfo +1 -1
- package/.turbo/turbo-build.log +8 -8
- package/.turbo/turbo-check-types.log +1 -1
- package/dist/index.cjs +43 -26
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +43 -26
- package/package.json +1 -1
- package/transformers/flatteners/flatten_accessors_of_type.ts +2 -0
- package/transformers/flatteners/flatten_json_value_to_type_paths_of.ts +2 -0
- package/transformers/flatteners/flatten_validation_errors_of_type.ts +2 -0
- package/transformers/flatteners/flatten_value_to.ts +35 -8
- package/transformers/flatteners/flatten_values_of_type.ts +2 -0
|
@@ -2,4 +2,4 @@ import { type Type } from 'types/definitions';
|
|
|
2
2
|
import { type Accessor, type FlattenedAccessorsOfType } from 'types/flattened_accessors_of_type';
|
|
3
3
|
import { type ValueOfType } from 'types/value_of_type';
|
|
4
4
|
import { type Setter } from './flatten_value_to';
|
|
5
|
-
export declare function flattenAccessorsOfType<T extends Type, R extends Readonly<Record<string, Accessor<any>>> = FlattenedAccessorsOfType<T>>(t: T, value: ValueOfType<T>, setValue: Setter<ValueOfType<T
|
|
5
|
+
export declare function flattenAccessorsOfType<T extends Type, R extends Readonly<Record<string, Accessor<any>>> = FlattenedAccessorsOfType<T>>(t: T, value: ValueOfType<T>, setValue: Setter<ValueOfType<T>>, listIndicesToKeys?: Record<string, number[]>): R;
|
|
@@ -5,7 +5,7 @@ function mapAccessor(_t, value, set) {
|
|
|
5
5
|
set,
|
|
6
6
|
};
|
|
7
7
|
}
|
|
8
|
-
export function flattenAccessorsOfType(t, value, setValue) {
|
|
8
|
+
export function flattenAccessorsOfType(t, value, setValue, listIndicesToKeys) {
|
|
9
9
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
-
return flattenValueTo(t, value, setValue, mapAccessor);
|
|
10
|
+
return flattenValueTo(t, value, setValue, mapAccessor, listIndicesToKeys);
|
|
11
11
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { type Type } from 'types/definitions';
|
|
2
2
|
import { type ValueOfType } from 'types/value_of_type';
|
|
3
|
-
export declare function flattenJsonValueToTypePathsOf<T extends Type, R extends Record<string, string | number | symbol>>(t: T, value: ValueOfType<T>): R;
|
|
3
|
+
export declare function flattenJsonValueToTypePathsOf<T extends Type, R extends Record<string, string | number | symbol>>(t: T, value: ValueOfType<T>, listIndicesToKeys?: Record<string, number[]>): R;
|
|
@@ -2,9 +2,12 @@ import { flattenValueTo, } from './flatten_value_to';
|
|
|
2
2
|
function mapTypePaths(_t, _value, _set, typePath) {
|
|
3
3
|
return typePath;
|
|
4
4
|
}
|
|
5
|
-
export function flattenJsonValueToTypePathsOf(t, value
|
|
5
|
+
export function flattenJsonValueToTypePathsOf(t, value,
|
|
6
|
+
// TODO
|
|
7
|
+
// : FlattenedJsonValueToTypePathsOf<T>
|
|
8
|
+
listIndicesToKeys) {
|
|
6
9
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
7
10
|
return flattenValueTo(t, value, function () {
|
|
8
11
|
// do nothing
|
|
9
|
-
}, mapTypePaths);
|
|
12
|
+
}, mapTypePaths, listIndicesToKeys);
|
|
10
13
|
}
|
|
@@ -18,5 +18,5 @@ export type ValuePathsToValidatorsOf<TypePathsToAdapters extends Partial<Readonl
|
|
|
18
18
|
export type FlattenedValidatorsOfType<T extends Type, Flattened extends Readonly<Record<string, Type>> = FlattenedTypesOfType<T, '*'>> = {
|
|
19
19
|
[K in keyof Flattened]: Validator;
|
|
20
20
|
};
|
|
21
|
-
export declare function flattenValidationErrorsOfType<T extends Type, ValueToTypePaths extends Readonly<Record<string, string>>, TypePathsToValidators extends FlattenedTypePathsToValidatorsOf<FlattenedValuesOfType<T, '*'>, ValueOfType<ReadonlyTypeOfType<T>>>, ValuePathsToValidators extends ValuePathsToValidatorsOf<TypePathsToValidators, ValueToTypePaths> = ValuePathsToValidatorsOf<TypePathsToValidators, ValueToTypePaths>>(type: T, value: ValueOfType<T>, validators: TypePathsToValidators): ErrorsOfFlattenedValidators<ValuePathsToValidators>;
|
|
21
|
+
export declare function flattenValidationErrorsOfType<T extends Type, ValueToTypePaths extends Readonly<Record<string, string>>, TypePathsToValidators extends FlattenedTypePathsToValidatorsOf<FlattenedValuesOfType<T, '*'>, ValueOfType<ReadonlyTypeOfType<T>>>, ValuePathsToValidators extends ValuePathsToValidatorsOf<TypePathsToValidators, ValueToTypePaths> = ValuePathsToValidatorsOf<TypePathsToValidators, ValueToTypePaths>>(type: T, value: ValueOfType<T>, validators: TypePathsToValidators, listIndicesToKeys?: Record<string, number[]>): ErrorsOfFlattenedValidators<ValuePathsToValidators>;
|
|
22
22
|
export {};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { flattenValueTo, } from 'transformers/flatteners/flatten_value_to';
|
|
2
2
|
import { validate, } from 'validation/validator';
|
|
3
|
-
export function flattenValidationErrorsOfType(type, value, validators) {
|
|
3
|
+
export function flattenValidationErrorsOfType(type, value, validators, listIndicesToKeys) {
|
|
4
4
|
return flattenValueTo(type, value, () => { }, function (_t, v, _setter, typePath, valuePath) {
|
|
5
5
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
6
6
|
const validator = validators[typePath];
|
|
7
7
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
8
8
|
return validator != null ? validate(validator, v, valuePath, value) : null;
|
|
9
|
-
});
|
|
9
|
+
}, listIndicesToKeys);
|
|
10
10
|
}
|
|
@@ -5,7 +5,7 @@ import { type ValueOfType } from 'types/value_of_type';
|
|
|
5
5
|
export type AnyValueType = any;
|
|
6
6
|
export type Setter<V> = (v: V) => void;
|
|
7
7
|
export type Mapper<R> = (t: StrictTypeDef, v: AnyValueType, setter: Setter<AnyValueType>, typePath: string, valuePath: string) => R;
|
|
8
|
-
export declare function flattenValueTo<T extends StrictType, M, R extends Readonly<Record<string, M>>>({ definition }: T, v: ValueOfType<T>, setter: Setter<ValueOfType<T>>, mapper: Mapper<M>): R;
|
|
8
|
+
export declare function flattenValueTo<T extends StrictType, M, R extends Readonly<Record<string, M>>>({ definition }: T, v: ValueOfType<T>, setter: Setter<ValueOfType<T>>, mapper: Mapper<M>, listIndicesToKeys?: Record<string, number[]>): R;
|
|
9
9
|
export declare function getUnionTypeDef<T extends UnionTypeDef>(typeDef: T, v: ValueOfType<ReadonlyTypeOfType<{
|
|
10
10
|
definition: T;
|
|
11
11
|
}>>): any;
|
|
@@ -1,66 +1,81 @@
|
|
|
1
1
|
import { reduce, UnreachableError, } from '@strictly/base';
|
|
2
2
|
import { TypeDefType, } from 'types/definitions';
|
|
3
3
|
import { jsonPath } from './json_path';
|
|
4
|
-
export function flattenValueTo({ definition }, v, setter, mapper
|
|
4
|
+
export function flattenValueTo({ definition }, v, setter, mapper,
|
|
5
|
+
// used to maintain keys when changing lists, note that the format for a list of three elements is
|
|
6
|
+
// [key1, key2, key3, nextKey]
|
|
7
|
+
// the final value always contains the next key
|
|
8
|
+
listIndicesToKeys = {}) {
|
|
5
9
|
const r = {};
|
|
6
|
-
internalFlattenValue('$', '$', definition, v, setter, mapper, r);
|
|
10
|
+
internalFlattenValue('$', '$', definition, v, setter, mapper, r, listIndicesToKeys);
|
|
7
11
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
8
12
|
return r;
|
|
9
13
|
}
|
|
10
|
-
function internalFlattenValue(valuePath, typePath, typeDef, v, setter, mapper, r) {
|
|
14
|
+
function internalFlattenValue(valuePath, typePath, typeDef, v, setter, mapper, r, listIndicesToKeys) {
|
|
11
15
|
r[valuePath] = mapper(typeDef, v, setter, typePath, valuePath);
|
|
12
16
|
// assume undefined means the field is optional and not populated
|
|
13
17
|
// TODO: actually capture if field is optional in typedef (or in builder for creating validator)
|
|
14
18
|
if (v !== undefined) {
|
|
15
|
-
return internalFlattenValueChildren(valuePath, typePath, typeDef, v, mapper, r);
|
|
19
|
+
return internalFlattenValueChildren(valuePath, typePath, typeDef, v, mapper, r, listIndicesToKeys);
|
|
16
20
|
}
|
|
17
21
|
return r;
|
|
18
22
|
}
|
|
19
|
-
function internalFlattenValueChildren(valuePath, typePath, typeDef, v, mapper, r) {
|
|
23
|
+
function internalFlattenValueChildren(valuePath, typePath, typeDef, v, mapper, r, listIndicesToKeys) {
|
|
20
24
|
switch (typeDef.type) {
|
|
21
25
|
case TypeDefType.Literal:
|
|
22
26
|
// no children
|
|
23
27
|
return r;
|
|
24
28
|
case TypeDefType.List:
|
|
25
|
-
return internalFlattenListChildren(valuePath, typePath, typeDef, v, mapper, r);
|
|
29
|
+
return internalFlattenListChildren(valuePath, typePath, typeDef, v, mapper, r, listIndicesToKeys);
|
|
26
30
|
case TypeDefType.Record:
|
|
27
|
-
return internalFlattenRecordChildren(valuePath, typePath, typeDef, v, mapper, r);
|
|
31
|
+
return internalFlattenRecordChildren(valuePath, typePath, typeDef, v, mapper, r, listIndicesToKeys);
|
|
28
32
|
case TypeDefType.Object:
|
|
29
|
-
return internalFlattenObjectChildren(valuePath, typePath, typeDef, v, mapper, r);
|
|
33
|
+
return internalFlattenObjectChildren(valuePath, typePath, typeDef, v, mapper, r, listIndicesToKeys);
|
|
30
34
|
case TypeDefType.Union:
|
|
31
|
-
return internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper, r);
|
|
35
|
+
return internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper, r, listIndicesToKeys);
|
|
32
36
|
default:
|
|
33
37
|
throw new UnreachableError(typeDef);
|
|
34
38
|
}
|
|
35
39
|
}
|
|
36
|
-
function internalFlattenListChildren(valuePath, typePath, { elements }, v, mapper, r) {
|
|
40
|
+
function internalFlattenListChildren(valuePath, typePath, { elements }, v, mapper, r, listIndicesToKeys) {
|
|
41
|
+
let indicesToKeys = listIndicesToKeys[valuePath];
|
|
42
|
+
if (indicesToKeys == null) {
|
|
43
|
+
indicesToKeys = [0];
|
|
44
|
+
listIndicesToKeys[valuePath] = indicesToKeys;
|
|
45
|
+
}
|
|
37
46
|
const newTypePath = jsonPath(typePath, '*');
|
|
38
|
-
return v.reduce(function (r, e,
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
47
|
+
return v.reduce(function (r, e, index) {
|
|
48
|
+
const key = indicesToKeys[index];
|
|
49
|
+
// we have consumed the next id passively
|
|
50
|
+
if (index === indicesToKeys.length - 1) {
|
|
51
|
+
// we have consumed the next key, so we need to add a new one
|
|
52
|
+
indicesToKeys.push(key + 1);
|
|
53
|
+
}
|
|
54
|
+
return internalFlattenValue(jsonPath(valuePath, key), newTypePath, elements, e, (e) => {
|
|
55
|
+
v[index] = e;
|
|
56
|
+
}, mapper, r, listIndicesToKeys);
|
|
42
57
|
}, r);
|
|
43
58
|
}
|
|
44
|
-
function internalFlattenRecordChildren(valuePath, typePath, { valueTypeDef }, v, mapper, r) {
|
|
59
|
+
function internalFlattenRecordChildren(valuePath, typePath, { valueTypeDef }, v, mapper, r, listIndicesToKeys) {
|
|
45
60
|
const newTypePath = jsonPath(typePath, '*');
|
|
46
61
|
return reduce(v, function (r, k, value) {
|
|
47
62
|
return internalFlattenValue(jsonPath(valuePath, k), newTypePath, valueTypeDef, value, (value) => {
|
|
48
63
|
v[k] = value;
|
|
49
|
-
}, mapper, r);
|
|
64
|
+
}, mapper, r, listIndicesToKeys);
|
|
50
65
|
}, r);
|
|
51
66
|
}
|
|
52
|
-
function internalFlattenObjectChildren(valuePath, typePath, { fields }, v, mapper, r) {
|
|
67
|
+
function internalFlattenObjectChildren(valuePath, typePath, { fields }, v, mapper, r, listIndicesToKeys) {
|
|
53
68
|
return reduce(fields, function (r, k, fieldTypeDef) {
|
|
54
69
|
const fieldValue = v[k];
|
|
55
70
|
return internalFlattenValue(jsonPath(valuePath, k), jsonPath(typePath, k), fieldTypeDef, fieldValue, (value) => {
|
|
56
71
|
v[k] = value;
|
|
57
|
-
}, mapper, r);
|
|
72
|
+
}, mapper, r, listIndicesToKeys);
|
|
58
73
|
}, r);
|
|
59
74
|
}
|
|
60
|
-
function internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper, r) {
|
|
75
|
+
function internalFlattenUnionChildren(valuePath, typePath, typeDef, v, mapper, r, listIndicesToKeys) {
|
|
61
76
|
const childTypeDef = getUnionTypeDef(typeDef, v);
|
|
62
77
|
const qualifier = typeDef.discriminator != null ? `:${v[typeDef.discriminator]}` : '';
|
|
63
|
-
return internalFlattenValueChildren(`${valuePath}${qualifier}`, `${typePath}${qualifier}`, childTypeDef, v, mapper, r);
|
|
78
|
+
return internalFlattenValueChildren(`${valuePath}${qualifier}`, `${typePath}${qualifier}`, childTypeDef, v, mapper, r, listIndicesToKeys);
|
|
64
79
|
}
|
|
65
80
|
export function getUnionTypeDef(typeDef, v) {
|
|
66
81
|
if (typeDef.discriminator == null) {
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { type Type } from 'types/definitions';
|
|
2
2
|
import { type ValueOfType } from 'types/value_of_type';
|
|
3
|
-
export declare function flattenValuesOfType<T extends Type>(typeDef: Type, value: ValueOfType<T>): Record<string, any>;
|
|
3
|
+
export declare function flattenValuesOfType<T extends Type>(typeDef: Type, value: ValueOfType<T>, listIndicesToKeys?: Record<string, number[]>): Record<string, any>;
|
|
@@ -2,6 +2,6 @@ import { flattenValueTo, } from './flatten_value_to';
|
|
|
2
2
|
function mapper(_t, v) {
|
|
3
3
|
return v;
|
|
4
4
|
}
|
|
5
|
-
export function flattenValuesOfType(typeDef, value) {
|
|
6
|
-
return flattenValueTo(typeDef, value, () => { }, mapper);
|
|
5
|
+
export function flattenValuesOfType(typeDef, value, listIndicesToKeys) {
|
|
6
|
+
return flattenValueTo(typeDef, value, () => { }, mapper, listIndicesToKeys);
|
|
7
7
|
}
|