@sprucelabs/schema 31.0.0 → 31.0.1
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 +10 -10
- package/build/esm/fields/SchemaField.types.d.ts +1 -5
- package/build/esm/schemas.static.types.d.ts +3 -3
- package/build/fields/SchemaField.js +10 -10
- package/build/fields/SchemaField.types.d.ts +1 -5
- package/build/schemas.static.types.d.ts +3 -3
- 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
|
});
|
|
@@ -260,6 +259,7 @@ class SchemaField extends AbstractField {
|
|
|
260
259
|
}
|
|
261
260
|
toValueType(value, options) {
|
|
262
261
|
const { createEntityInstances, schemasById: schemasById = {} } = options || {};
|
|
262
|
+
debugger;
|
|
263
263
|
// try and pull the schema definition from the options and by id
|
|
264
264
|
const destinationSchemas = SchemaField.mapFieldDefinitionToSchemas(this.definition, {
|
|
265
265
|
schemasById,
|
|
@@ -275,8 +275,8 @@ class SchemaField extends AbstractField {
|
|
|
275
275
|
}
|
|
276
276
|
else {
|
|
277
277
|
// this could be one of a few types, lets check the "schemaId" prop
|
|
278
|
-
const {
|
|
279
|
-
const allMatches = destinationSchemas.filter((def) => def.id ===
|
|
278
|
+
const { id, values } = value;
|
|
279
|
+
const allMatches = destinationSchemas.filter((def) => def.id === id);
|
|
280
280
|
let matchedSchema;
|
|
281
281
|
if (allMatches.length === 0) {
|
|
282
282
|
throw new SpruceError({
|
|
@@ -294,7 +294,7 @@ class SchemaField extends AbstractField {
|
|
|
294
294
|
if (!matchedSchema) {
|
|
295
295
|
throw new SpruceError({
|
|
296
296
|
code: 'VERSION_NOT_FOUND',
|
|
297
|
-
schemaId,
|
|
297
|
+
schemaId: id,
|
|
298
298
|
});
|
|
299
299
|
}
|
|
300
300
|
}
|
|
@@ -313,7 +313,7 @@ class SchemaField extends AbstractField {
|
|
|
313
313
|
};
|
|
314
314
|
if (isUnion) {
|
|
315
315
|
return {
|
|
316
|
-
|
|
316
|
+
id: instance.schemaId,
|
|
317
317
|
values: instance.getValues(getValueOptions),
|
|
318
318
|
};
|
|
319
319
|
}
|
|
@@ -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> = {
|
|
@@ -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
|
});
|
|
@@ -264,6 +263,7 @@ class SchemaField extends AbstractField_1.default {
|
|
|
264
263
|
}
|
|
265
264
|
toValueType(value, options) {
|
|
266
265
|
const { createEntityInstances, schemasById: schemasById = {} } = options || {};
|
|
266
|
+
debugger;
|
|
267
267
|
// try and pull the schema definition from the options and by id
|
|
268
268
|
const destinationSchemas = SchemaField.mapFieldDefinitionToSchemas(this.definition, {
|
|
269
269
|
schemasById,
|
|
@@ -279,8 +279,8 @@ class SchemaField extends AbstractField_1.default {
|
|
|
279
279
|
}
|
|
280
280
|
else {
|
|
281
281
|
// this could be one of a few types, lets check the "schemaId" prop
|
|
282
|
-
const {
|
|
283
|
-
const allMatches = destinationSchemas.filter((def) => def.id ===
|
|
282
|
+
const { id, values } = value;
|
|
283
|
+
const allMatches = destinationSchemas.filter((def) => def.id === id);
|
|
284
284
|
let matchedSchema;
|
|
285
285
|
if (allMatches.length === 0) {
|
|
286
286
|
throw new SpruceError_1.default({
|
|
@@ -298,7 +298,7 @@ class SchemaField extends AbstractField_1.default {
|
|
|
298
298
|
if (!matchedSchema) {
|
|
299
299
|
throw new SpruceError_1.default({
|
|
300
300
|
code: 'VERSION_NOT_FOUND',
|
|
301
|
-
schemaId,
|
|
301
|
+
schemaId: id,
|
|
302
302
|
});
|
|
303
303
|
}
|
|
304
304
|
}
|
|
@@ -317,7 +317,7 @@ class SchemaField extends AbstractField_1.default {
|
|
|
317
317
|
};
|
|
318
318
|
if (isUnion) {
|
|
319
319
|
return {
|
|
320
|
-
|
|
320
|
+
id: instance.schemaId,
|
|
321
321
|
values: instance.getValues(getValueOptions),
|
|
322
322
|
};
|
|
323
323
|
}
|
|
@@ -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> = {
|