@sprucelabs/schema 30.0.572 → 30.0.573
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/build/StaticSchemaEntityImpl.js +22 -1
- package/build/esm/StaticSchemaEntityImpl.js +22 -1
- package/build/esm/schemas.static.types.d.ts +1 -0
- package/build/esm/types/utilities.types.d.ts +42 -0
- package/build/esm/utilities/normalizePartialSchemaValues.js +1 -0
- package/build/esm/utilities/normalizeSchemaValues.d.ts +2 -1
- package/build/esm/utilities/normalizeSchemaValues.js +11 -2
- package/build/schemas.static.types.d.ts +1 -0
- package/build/types/utilities.types.d.ts +42 -0
- package/build/utilities/normalizePartialSchemaValues.js +1 -0
- package/build/utilities/normalizeSchemaValues.d.ts +2 -1
- package/build/utilities/normalizeSchemaValues.js +11 -2
- package/package.json +1 -1
|
@@ -50,7 +50,7 @@ class StaticSchemaEntityImpl extends AbstractEntity_1.default {
|
|
|
50
50
|
this.buildFields();
|
|
51
51
|
const v = {
|
|
52
52
|
...this.values,
|
|
53
|
-
...values,
|
|
53
|
+
...expandValues(values),
|
|
54
54
|
};
|
|
55
55
|
this.values = (0, cloneDeepPreservingInstances_1.default)(v);
|
|
56
56
|
}
|
|
@@ -215,3 +215,24 @@ class StaticSchemaEntityImpl extends AbstractEntity_1.default {
|
|
|
215
215
|
}
|
|
216
216
|
StaticSchemaEntityImpl.enableDuplicateCheckWhenTracking = true;
|
|
217
217
|
exports.default = StaticSchemaEntityImpl;
|
|
218
|
+
function expandValues(values = {}) {
|
|
219
|
+
const result = {};
|
|
220
|
+
for (const key in values) {
|
|
221
|
+
const value = values[key];
|
|
222
|
+
const keys = key.split('.');
|
|
223
|
+
let current = result;
|
|
224
|
+
for (let i = 0; i < keys.length; i++) {
|
|
225
|
+
const k = keys[i];
|
|
226
|
+
if (i === keys.length - 1) {
|
|
227
|
+
current[k] = value;
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
if (!(k in current) || typeof current[k] !== 'object') {
|
|
231
|
+
current[k] = {};
|
|
232
|
+
}
|
|
233
|
+
current = current[k];
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return result;
|
|
238
|
+
}
|
|
@@ -12,7 +12,7 @@ class StaticSchemaEntityImpl extends AbstractEntity {
|
|
|
12
12
|
this.buildFields();
|
|
13
13
|
const v = {
|
|
14
14
|
...this.values,
|
|
15
|
-
...values,
|
|
15
|
+
...expandValues(values),
|
|
16
16
|
};
|
|
17
17
|
this.values = cloneDeepPreservingInstances(v);
|
|
18
18
|
}
|
|
@@ -179,3 +179,24 @@ class StaticSchemaEntityImpl extends AbstractEntity {
|
|
|
179
179
|
}
|
|
180
180
|
StaticSchemaEntityImpl.enableDuplicateCheckWhenTracking = true;
|
|
181
181
|
export default StaticSchemaEntityImpl;
|
|
182
|
+
function expandValues(values = {}) {
|
|
183
|
+
const result = {};
|
|
184
|
+
for (const key in values) {
|
|
185
|
+
const value = values[key];
|
|
186
|
+
const keys = key.split('.');
|
|
187
|
+
let current = result;
|
|
188
|
+
for (let i = 0; i < keys.length; i++) {
|
|
189
|
+
const k = keys[i];
|
|
190
|
+
if (i === keys.length - 1) {
|
|
191
|
+
current[k] = value;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
if (!(k in current) || typeof current[k] !== 'object') {
|
|
195
|
+
current[k] = {};
|
|
196
|
+
}
|
|
197
|
+
current = current[k];
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
@@ -117,6 +117,7 @@ export interface SchemaNormalizeOptions<S extends Schema, CreateEntityInstances
|
|
|
117
117
|
[K in SchemaFieldNames<S>]?: S['fields'][K] extends FieldDefinition ? Partial<FieldDefinitionMap[S['fields'][K]['type']]['options']> : never;
|
|
118
118
|
};
|
|
119
119
|
shouldIncludeNullAndUndefinedFields?: ShouldIncludeNullAndUndefinedFields;
|
|
120
|
+
shouldRetainDotSyntaxKeys?: boolean;
|
|
120
121
|
}
|
|
121
122
|
export interface DynamicSchemaNormalizeOptions<CreateEntityInstances extends boolean> extends SchemaNormalizeFieldValueOptions<CreateEntityInstances> {
|
|
122
123
|
}
|
|
@@ -4,3 +4,45 @@ export type IsArray<T, IsArray> = IsArray extends true ? Unpack<T>[] : Unpack<T>
|
|
|
4
4
|
export type IsArrayNoUnpack<T, IsArray> = IsArray extends true ? T[] : T;
|
|
5
5
|
export type IsRequired<T, IsRequired> = IsRequired extends true ? T : T | undefined | null;
|
|
6
6
|
export type AreAnyFieldsRequired<S extends Schema | undefined> = S extends Schema ? SchemaRequiredFieldNames<S> extends [] ? false : true : false;
|
|
7
|
+
export type ValuesWithPaths<Values, Keys extends Paths<Values> = Paths<Values>> = {
|
|
8
|
+
[K in RequiredKeys<Values, Keys>]: TypeAtPath<Values, K>;
|
|
9
|
+
} & {
|
|
10
|
+
[K in OptionalKeys<Values, Keys>]?: TypeAtPath<Values, K>;
|
|
11
|
+
};
|
|
12
|
+
type IsOptional<T, K extends keyof T> = T extends any ? {} extends Pick<T, K> ? true : false : never;
|
|
13
|
+
type IsPathOptional<T, P extends string> = T extends any ? P extends `${infer K}.${infer Rest}` ? K extends keyof T ? IsOptional<T, K> extends true ? true : IsPathOptional<T[K], Rest> : true : P extends keyof T ? IsOptional<T, P> : true : true;
|
|
14
|
+
type RequiredKeys<Values, Keys extends Paths<Values>> = {
|
|
15
|
+
[K in Keys]: IsPathOptional<Values, K> extends true ? never : K;
|
|
16
|
+
}[Keys];
|
|
17
|
+
type OptionalKeys<Values, Keys extends Paths<Values>> = Exclude<Keys, RequiredKeys<Values, Keys>>;
|
|
18
|
+
type Paths<T, D extends number = 3> = [D] extends [never] ? never : T extends object ? {
|
|
19
|
+
[K in keyof T]-?: K extends string | number ? `${K}` | Join<K, Paths<T[K], Prev[D]>> : never;
|
|
20
|
+
}[keyof T] : '';
|
|
21
|
+
type TypeAtPath<T, P extends string> = T extends any ? T extends null | undefined ? undefined : P extends `${infer K}.${infer Rest}` ? K extends keyof T ? TypeAtPath<T[K], Rest> : any : P extends keyof T ? T[P] : any : never;
|
|
22
|
+
type Join<K, P> = K extends string | number ? P extends string | number ? `${K}${'' extends P ? '' : '.'}${P}` : never : never;
|
|
23
|
+
type Prev = [
|
|
24
|
+
never,
|
|
25
|
+
0,
|
|
26
|
+
1,
|
|
27
|
+
2,
|
|
28
|
+
3,
|
|
29
|
+
4,
|
|
30
|
+
5,
|
|
31
|
+
6,
|
|
32
|
+
7,
|
|
33
|
+
8,
|
|
34
|
+
9,
|
|
35
|
+
10,
|
|
36
|
+
11,
|
|
37
|
+
12,
|
|
38
|
+
13,
|
|
39
|
+
14,
|
|
40
|
+
15,
|
|
41
|
+
16,
|
|
42
|
+
17,
|
|
43
|
+
18,
|
|
44
|
+
19,
|
|
45
|
+
20,
|
|
46
|
+
...0[]
|
|
47
|
+
];
|
|
48
|
+
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import normalizeSchemaValues from './normalizeSchemaValues.js';
|
|
2
2
|
export default function normalizePartialSchemaValues(schema, values, options) {
|
|
3
|
+
//@ts-ignore
|
|
3
4
|
const normalized = normalizeSchemaValues(schema, values, {
|
|
4
5
|
...options,
|
|
5
6
|
shouldIncludeNullAndUndefinedFields: false,
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { Schema, SchemaPartialValues, SchemaGetValuesOptions, SchemaFieldNames, SchemaPublicFieldNames, IsDynamicSchema, DynamicSchemaAllValues, SchemaValues } from '../schemas.static.types';
|
|
2
|
-
|
|
2
|
+
import { ValuesWithPaths } from '../types/utilities.types';
|
|
3
|
+
export default function normalizeSchemaValues<S extends Schema, F extends SchemaFieldNames<S> = SchemaFieldNames<S>, PF extends SchemaPublicFieldNames<S> = SchemaPublicFieldNames<S>, CreateEntityInstances extends boolean = false, IncludePrivateFields extends boolean = true, IsDynamic extends boolean = IsDynamicSchema<S>, ShouldIncludeNullAndUndefinedFields extends boolean = true>(schema: S, values: ValuesWithPaths<SchemaPartialValues<S>>, options?: SchemaGetValuesOptions<S, F, PF, CreateEntityInstances, IncludePrivateFields, ShouldIncludeNullAndUndefinedFields>): IsDynamic extends true ? DynamicSchemaAllValues<S, CreateEntityInstances> : IncludePrivateFields extends true ? Pick<SchemaValues<S, CreateEntityInstances, true, ShouldIncludeNullAndUndefinedFields>, F> : Pick<SchemaValues<S, CreateEntityInstances, true, ShouldIncludeNullAndUndefinedFields>, PF>;
|
|
@@ -1,10 +1,19 @@
|
|
|
1
|
+
import get from 'just-safe-get';
|
|
1
2
|
import EntityFactory from '../factories/SchemaEntityFactory.js';
|
|
2
3
|
export default function normalizeSchemaValues(schema, values, options) {
|
|
3
4
|
const instance = EntityFactory.Entity(schema, values);
|
|
4
|
-
const { shouldCreateEntityInstances = false, ...rest } = options || {};
|
|
5
|
+
const { shouldCreateEntityInstances = false, shouldRetainDotSyntaxKeys, ...rest } = options || {};
|
|
5
6
|
const normalizedOptions = {
|
|
6
7
|
shouldCreateEntityInstances,
|
|
7
8
|
...rest,
|
|
8
9
|
};
|
|
9
|
-
|
|
10
|
+
let normalized = instance.getValues(normalizedOptions);
|
|
11
|
+
if (shouldRetainDotSyntaxKeys) {
|
|
12
|
+
const normalizedWithKeys = {};
|
|
13
|
+
for (const key of Object.keys(values)) {
|
|
14
|
+
normalizedWithKeys[key] = get(normalized, key);
|
|
15
|
+
}
|
|
16
|
+
normalized = normalizedWithKeys;
|
|
17
|
+
}
|
|
18
|
+
return normalized;
|
|
10
19
|
}
|
|
@@ -117,6 +117,7 @@ export interface SchemaNormalizeOptions<S extends Schema, CreateEntityInstances
|
|
|
117
117
|
[K in SchemaFieldNames<S>]?: S['fields'][K] extends FieldDefinition ? Partial<FieldDefinitionMap[S['fields'][K]['type']]['options']> : never;
|
|
118
118
|
};
|
|
119
119
|
shouldIncludeNullAndUndefinedFields?: ShouldIncludeNullAndUndefinedFields;
|
|
120
|
+
shouldRetainDotSyntaxKeys?: boolean;
|
|
120
121
|
}
|
|
121
122
|
export interface DynamicSchemaNormalizeOptions<CreateEntityInstances extends boolean> extends SchemaNormalizeFieldValueOptions<CreateEntityInstances> {
|
|
122
123
|
}
|
|
@@ -4,3 +4,45 @@ export type IsArray<T, IsArray> = IsArray extends true ? Unpack<T>[] : Unpack<T>
|
|
|
4
4
|
export type IsArrayNoUnpack<T, IsArray> = IsArray extends true ? T[] : T;
|
|
5
5
|
export type IsRequired<T, IsRequired> = IsRequired extends true ? T : T | undefined | null;
|
|
6
6
|
export type AreAnyFieldsRequired<S extends Schema | undefined> = S extends Schema ? SchemaRequiredFieldNames<S> extends [] ? false : true : false;
|
|
7
|
+
export type ValuesWithPaths<Values, Keys extends Paths<Values> = Paths<Values>> = {
|
|
8
|
+
[K in RequiredKeys<Values, Keys>]: TypeAtPath<Values, K>;
|
|
9
|
+
} & {
|
|
10
|
+
[K in OptionalKeys<Values, Keys>]?: TypeAtPath<Values, K>;
|
|
11
|
+
};
|
|
12
|
+
type IsOptional<T, K extends keyof T> = T extends any ? {} extends Pick<T, K> ? true : false : never;
|
|
13
|
+
type IsPathOptional<T, P extends string> = T extends any ? P extends `${infer K}.${infer Rest}` ? K extends keyof T ? IsOptional<T, K> extends true ? true : IsPathOptional<T[K], Rest> : true : P extends keyof T ? IsOptional<T, P> : true : true;
|
|
14
|
+
type RequiredKeys<Values, Keys extends Paths<Values>> = {
|
|
15
|
+
[K in Keys]: IsPathOptional<Values, K> extends true ? never : K;
|
|
16
|
+
}[Keys];
|
|
17
|
+
type OptionalKeys<Values, Keys extends Paths<Values>> = Exclude<Keys, RequiredKeys<Values, Keys>>;
|
|
18
|
+
type Paths<T, D extends number = 3> = [D] extends [never] ? never : T extends object ? {
|
|
19
|
+
[K in keyof T]-?: K extends string | number ? `${K}` | Join<K, Paths<T[K], Prev[D]>> : never;
|
|
20
|
+
}[keyof T] : '';
|
|
21
|
+
type TypeAtPath<T, P extends string> = T extends any ? T extends null | undefined ? undefined : P extends `${infer K}.${infer Rest}` ? K extends keyof T ? TypeAtPath<T[K], Rest> : any : P extends keyof T ? T[P] : any : never;
|
|
22
|
+
type Join<K, P> = K extends string | number ? P extends string | number ? `${K}${'' extends P ? '' : '.'}${P}` : never : never;
|
|
23
|
+
type Prev = [
|
|
24
|
+
never,
|
|
25
|
+
0,
|
|
26
|
+
1,
|
|
27
|
+
2,
|
|
28
|
+
3,
|
|
29
|
+
4,
|
|
30
|
+
5,
|
|
31
|
+
6,
|
|
32
|
+
7,
|
|
33
|
+
8,
|
|
34
|
+
9,
|
|
35
|
+
10,
|
|
36
|
+
11,
|
|
37
|
+
12,
|
|
38
|
+
13,
|
|
39
|
+
14,
|
|
40
|
+
15,
|
|
41
|
+
16,
|
|
42
|
+
17,
|
|
43
|
+
18,
|
|
44
|
+
19,
|
|
45
|
+
20,
|
|
46
|
+
...0[]
|
|
47
|
+
];
|
|
48
|
+
export {};
|
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.default = normalizePartialSchemaValues;
|
|
7
7
|
const normalizeSchemaValues_1 = __importDefault(require("./normalizeSchemaValues"));
|
|
8
8
|
function normalizePartialSchemaValues(schema, values, options) {
|
|
9
|
+
//@ts-ignore
|
|
9
10
|
const normalized = (0, normalizeSchemaValues_1.default)(schema, values, {
|
|
10
11
|
...options,
|
|
11
12
|
shouldIncludeNullAndUndefinedFields: false,
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { Schema, SchemaPartialValues, SchemaGetValuesOptions, SchemaFieldNames, SchemaPublicFieldNames, IsDynamicSchema, DynamicSchemaAllValues, SchemaValues } from '../schemas.static.types';
|
|
2
|
-
|
|
2
|
+
import { ValuesWithPaths } from '../types/utilities.types';
|
|
3
|
+
export default function normalizeSchemaValues<S extends Schema, F extends SchemaFieldNames<S> = SchemaFieldNames<S>, PF extends SchemaPublicFieldNames<S> = SchemaPublicFieldNames<S>, CreateEntityInstances extends boolean = false, IncludePrivateFields extends boolean = true, IsDynamic extends boolean = IsDynamicSchema<S>, ShouldIncludeNullAndUndefinedFields extends boolean = true>(schema: S, values: ValuesWithPaths<SchemaPartialValues<S>>, options?: SchemaGetValuesOptions<S, F, PF, CreateEntityInstances, IncludePrivateFields, ShouldIncludeNullAndUndefinedFields>): IsDynamic extends true ? DynamicSchemaAllValues<S, CreateEntityInstances> : IncludePrivateFields extends true ? Pick<SchemaValues<S, CreateEntityInstances, true, ShouldIncludeNullAndUndefinedFields>, F> : Pick<SchemaValues<S, CreateEntityInstances, true, ShouldIncludeNullAndUndefinedFields>, PF>;
|
|
@@ -4,13 +4,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.default = normalizeSchemaValues;
|
|
7
|
+
const just_safe_get_1 = __importDefault(require("just-safe-get"));
|
|
7
8
|
const SchemaEntityFactory_1 = __importDefault(require("../factories/SchemaEntityFactory"));
|
|
8
9
|
function normalizeSchemaValues(schema, values, options) {
|
|
9
10
|
const instance = SchemaEntityFactory_1.default.Entity(schema, values);
|
|
10
|
-
const { shouldCreateEntityInstances = false, ...rest } = options || {};
|
|
11
|
+
const { shouldCreateEntityInstances = false, shouldRetainDotSyntaxKeys, ...rest } = options || {};
|
|
11
12
|
const normalizedOptions = {
|
|
12
13
|
shouldCreateEntityInstances,
|
|
13
14
|
...rest,
|
|
14
15
|
};
|
|
15
|
-
|
|
16
|
+
let normalized = instance.getValues(normalizedOptions);
|
|
17
|
+
if (shouldRetainDotSyntaxKeys) {
|
|
18
|
+
const normalizedWithKeys = {};
|
|
19
|
+
for (const key of Object.keys(values)) {
|
|
20
|
+
normalizedWithKeys[key] = (0, just_safe_get_1.default)(normalized, key);
|
|
21
|
+
}
|
|
22
|
+
normalized = normalizedWithKeys;
|
|
23
|
+
}
|
|
24
|
+
return normalized;
|
|
16
25
|
}
|