@sprucelabs/schema 31.0.0 → 31.0.2
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/esm/fields/SchemaField.js +9 -10
- package/build/esm/fields/SchemaField.types.d.ts +1 -5
- package/build/esm/schemas.static.types.d.ts +3 -3
- package/build/esm/utilities/normalizeSchemaValues.d.ts +1 -1
- package/build/fields/SchemaField.js +9 -10
- package/build/fields/SchemaField.types.d.ts +1 -5
- package/build/schemas.static.types.d.ts +3 -3
- package/build/utilities/normalizeSchemaValues.d.ts +1 -1
- package/package.json +1 -1
|
@@ -197,7 +197,7 @@ class SchemaField extends AbstractField {
|
|
|
197
197
|
instance = this.Schema(schemas[0], value);
|
|
198
198
|
}
|
|
199
199
|
else if (schemas && schemas.length > 0) {
|
|
200
|
-
const {
|
|
200
|
+
const { id, version, values } = value || {};
|
|
201
201
|
if (!values) {
|
|
202
202
|
errors.push({
|
|
203
203
|
name: this.name,
|
|
@@ -207,24 +207,23 @@ class SchemaField extends AbstractField {
|
|
|
207
207
|
this.name,
|
|
208
208
|
});
|
|
209
209
|
}
|
|
210
|
-
else if (!
|
|
210
|
+
else if (!id) {
|
|
211
211
|
errors.push({
|
|
212
212
|
name: this.name,
|
|
213
213
|
label: this.label,
|
|
214
214
|
code: 'INVALID_PARAMETER',
|
|
215
|
-
friendlyMessage: 'You need to add `
|
|
215
|
+
friendlyMessage: 'You need to add `id` to the value of ' +
|
|
216
216
|
this.name,
|
|
217
217
|
});
|
|
218
218
|
}
|
|
219
219
|
else {
|
|
220
|
-
const matchSchema = schemas.find((schema) => schema.id ===
|
|
221
|
-
schema.version === version);
|
|
220
|
+
const matchSchema = schemas.find((schema) => schema.id === id && schema.version === version);
|
|
222
221
|
if (!matchSchema) {
|
|
223
222
|
errors.push({
|
|
224
223
|
name: this.name,
|
|
225
224
|
label: this.label,
|
|
226
225
|
code: 'INVALID_PARAMETER',
|
|
227
|
-
friendlyMessage: `Could not find a schema by id '${
|
|
226
|
+
friendlyMessage: `Could not find a schema by id '${id}'${version
|
|
228
227
|
? ` and version '${version}'`
|
|
229
228
|
: ' with no version'}.`,
|
|
230
229
|
});
|
|
@@ -275,8 +274,8 @@ class SchemaField extends AbstractField {
|
|
|
275
274
|
}
|
|
276
275
|
else {
|
|
277
276
|
// this could be one of a few types, lets check the "schemaId" prop
|
|
278
|
-
const {
|
|
279
|
-
const allMatches = destinationSchemas.filter((def) => def.id ===
|
|
277
|
+
const { id, values } = value;
|
|
278
|
+
const allMatches = destinationSchemas.filter((def) => def.id === id);
|
|
280
279
|
let matchedSchema;
|
|
281
280
|
if (allMatches.length === 0) {
|
|
282
281
|
throw new SpruceError({
|
|
@@ -294,7 +293,7 @@ class SchemaField extends AbstractField {
|
|
|
294
293
|
if (!matchedSchema) {
|
|
295
294
|
throw new SpruceError({
|
|
296
295
|
code: 'VERSION_NOT_FOUND',
|
|
297
|
-
schemaId,
|
|
296
|
+
schemaId: id,
|
|
298
297
|
});
|
|
299
298
|
}
|
|
300
299
|
}
|
|
@@ -313,7 +312,7 @@ class SchemaField extends AbstractField {
|
|
|
313
312
|
};
|
|
314
313
|
if (isUnion) {
|
|
315
314
|
return {
|
|
316
|
-
|
|
315
|
+
id: instance.schemaId,
|
|
317
316
|
values: instance.getValues(getValueOptions),
|
|
318
317
|
};
|
|
319
318
|
}
|
|
@@ -16,11 +16,7 @@ export interface SchemaFieldOptions {
|
|
|
16
16
|
typeSuffix?: string;
|
|
17
17
|
}
|
|
18
18
|
export type SchemaFieldUnion<S extends Schema[], CreateEntityInstances extends boolean = false> = {
|
|
19
|
-
[K in keyof S]: S[K] extends Schema ? CreateEntityInstances extends true ? StaticSchemaEntity<S[K]> :
|
|
20
|
-
schemaId: S[K]['id'];
|
|
21
|
-
version?: S[K]['version'];
|
|
22
|
-
values: SchemaValues<S[K]>;
|
|
23
|
-
} : any;
|
|
19
|
+
[K in keyof S]: S[K] extends Schema ? CreateEntityInstances extends true ? StaticSchemaEntity<S[K]> : SchemaFieldValueUnion<SchemaValues<S[K]>, S[K]['id'], S[K]['version']> : any;
|
|
24
20
|
};
|
|
25
21
|
export type SchemaFieldValueTypeMapper<F extends SchemaFieldFieldDefinition, CreateEntityInstances extends boolean = false, ShouldIncludeNullAndUndefinedFields extends boolean = false> = F['options']['schemas'] extends Schema[] ? IsArrayNoUnpack<SchemaFieldUnion<F['options']['schemas'], CreateEntityInstances>[number], F['isArray']> : F['options']['schema'] extends Schema ? CreateEntityInstances extends true ? IsArray<StaticSchemaEntity<F['options']['schema']>, F['isArray']> : IsArray<SchemaValues<F['options']['schema'], false, true, ShouldIncludeNullAndUndefinedFields>, F['isArray']> : any;
|
|
26
22
|
export type SchemaFieldFieldDefinition = FieldDefinition<Record<string, any>, Record<string, any>, SchemaFieldValueUnion[], SchemaFieldValueUnion[]> & {
|
|
@@ -56,9 +56,9 @@ export interface Schema {
|
|
|
56
56
|
};
|
|
57
57
|
fields?: SchemaFieldsByName;
|
|
58
58
|
}
|
|
59
|
-
export interface SchemaFieldValueUnion<V extends Record<string, any> = Record<string, any
|
|
60
|
-
|
|
61
|
-
version?:
|
|
59
|
+
export interface SchemaFieldValueUnion<V extends Record<string, any> = Record<string, any>, Id extends string = string, Version extends string | undefined = undefined> {
|
|
60
|
+
id: Id;
|
|
61
|
+
version?: Version;
|
|
62
62
|
values: V;
|
|
63
63
|
}
|
|
64
64
|
export type SchemaFields<T extends Schema> = {
|
|
@@ -1,3 +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
|
|
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>> | SchemaValues<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>;
|
|
@@ -201,7 +201,7 @@ class SchemaField extends AbstractField_1.default {
|
|
|
201
201
|
instance = this.Schema(schemas[0], value);
|
|
202
202
|
}
|
|
203
203
|
else if (schemas && schemas.length > 0) {
|
|
204
|
-
const {
|
|
204
|
+
const { id, version, values } = value || {};
|
|
205
205
|
if (!values) {
|
|
206
206
|
errors.push({
|
|
207
207
|
name: this.name,
|
|
@@ -211,24 +211,23 @@ class SchemaField extends AbstractField_1.default {
|
|
|
211
211
|
this.name,
|
|
212
212
|
});
|
|
213
213
|
}
|
|
214
|
-
else if (!
|
|
214
|
+
else if (!id) {
|
|
215
215
|
errors.push({
|
|
216
216
|
name: this.name,
|
|
217
217
|
label: this.label,
|
|
218
218
|
code: 'INVALID_PARAMETER',
|
|
219
|
-
friendlyMessage: 'You need to add `
|
|
219
|
+
friendlyMessage: 'You need to add `id` to the value of ' +
|
|
220
220
|
this.name,
|
|
221
221
|
});
|
|
222
222
|
}
|
|
223
223
|
else {
|
|
224
|
-
const matchSchema = schemas.find((schema) => schema.id ===
|
|
225
|
-
schema.version === version);
|
|
224
|
+
const matchSchema = schemas.find((schema) => schema.id === id && schema.version === version);
|
|
226
225
|
if (!matchSchema) {
|
|
227
226
|
errors.push({
|
|
228
227
|
name: this.name,
|
|
229
228
|
label: this.label,
|
|
230
229
|
code: 'INVALID_PARAMETER',
|
|
231
|
-
friendlyMessage: `Could not find a schema by id '${
|
|
230
|
+
friendlyMessage: `Could not find a schema by id '${id}'${version
|
|
232
231
|
? ` and version '${version}'`
|
|
233
232
|
: ' with no version'}.`,
|
|
234
233
|
});
|
|
@@ -279,8 +278,8 @@ class SchemaField extends AbstractField_1.default {
|
|
|
279
278
|
}
|
|
280
279
|
else {
|
|
281
280
|
// this could be one of a few types, lets check the "schemaId" prop
|
|
282
|
-
const {
|
|
283
|
-
const allMatches = destinationSchemas.filter((def) => def.id ===
|
|
281
|
+
const { id, values } = value;
|
|
282
|
+
const allMatches = destinationSchemas.filter((def) => def.id === id);
|
|
284
283
|
let matchedSchema;
|
|
285
284
|
if (allMatches.length === 0) {
|
|
286
285
|
throw new SpruceError_1.default({
|
|
@@ -298,7 +297,7 @@ class SchemaField extends AbstractField_1.default {
|
|
|
298
297
|
if (!matchedSchema) {
|
|
299
298
|
throw new SpruceError_1.default({
|
|
300
299
|
code: 'VERSION_NOT_FOUND',
|
|
301
|
-
schemaId,
|
|
300
|
+
schemaId: id,
|
|
302
301
|
});
|
|
303
302
|
}
|
|
304
303
|
}
|
|
@@ -317,7 +316,7 @@ class SchemaField extends AbstractField_1.default {
|
|
|
317
316
|
};
|
|
318
317
|
if (isUnion) {
|
|
319
318
|
return {
|
|
320
|
-
|
|
319
|
+
id: instance.schemaId,
|
|
321
320
|
values: instance.getValues(getValueOptions),
|
|
322
321
|
};
|
|
323
322
|
}
|
|
@@ -16,11 +16,7 @@ export interface SchemaFieldOptions {
|
|
|
16
16
|
typeSuffix?: string;
|
|
17
17
|
}
|
|
18
18
|
export type SchemaFieldUnion<S extends Schema[], CreateEntityInstances extends boolean = false> = {
|
|
19
|
-
[K in keyof S]: S[K] extends Schema ? CreateEntityInstances extends true ? StaticSchemaEntity<S[K]> :
|
|
20
|
-
schemaId: S[K]['id'];
|
|
21
|
-
version?: S[K]['version'];
|
|
22
|
-
values: SchemaValues<S[K]>;
|
|
23
|
-
} : any;
|
|
19
|
+
[K in keyof S]: S[K] extends Schema ? CreateEntityInstances extends true ? StaticSchemaEntity<S[K]> : SchemaFieldValueUnion<SchemaValues<S[K]>, S[K]['id'], S[K]['version']> : any;
|
|
24
20
|
};
|
|
25
21
|
export type SchemaFieldValueTypeMapper<F extends SchemaFieldFieldDefinition, CreateEntityInstances extends boolean = false, ShouldIncludeNullAndUndefinedFields extends boolean = false> = F['options']['schemas'] extends Schema[] ? IsArrayNoUnpack<SchemaFieldUnion<F['options']['schemas'], CreateEntityInstances>[number], F['isArray']> : F['options']['schema'] extends Schema ? CreateEntityInstances extends true ? IsArray<StaticSchemaEntity<F['options']['schema']>, F['isArray']> : IsArray<SchemaValues<F['options']['schema'], false, true, ShouldIncludeNullAndUndefinedFields>, F['isArray']> : any;
|
|
26
22
|
export type SchemaFieldFieldDefinition = FieldDefinition<Record<string, any>, Record<string, any>, SchemaFieldValueUnion[], SchemaFieldValueUnion[]> & {
|
|
@@ -56,9 +56,9 @@ export interface Schema {
|
|
|
56
56
|
};
|
|
57
57
|
fields?: SchemaFieldsByName;
|
|
58
58
|
}
|
|
59
|
-
export interface SchemaFieldValueUnion<V extends Record<string, any> = Record<string, any
|
|
60
|
-
|
|
61
|
-
version?:
|
|
59
|
+
export interface SchemaFieldValueUnion<V extends Record<string, any> = Record<string, any>, Id extends string = string, Version extends string | undefined = undefined> {
|
|
60
|
+
id: Id;
|
|
61
|
+
version?: Version;
|
|
62
62
|
values: V;
|
|
63
63
|
}
|
|
64
64
|
export type SchemaFields<T extends Schema> = {
|
|
@@ -1,3 +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
|
|
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>> | SchemaValues<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>;
|