@sprucelabs/schema 29.4.59 → 30.0.0
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/.spruce/schemas/fields/fieldClassMap.d.ts +2 -2
- package/build/.spruce/schemas/fields/fieldClassMap.js +3 -3
- package/build/AbstractEntity.js +1 -0
- package/build/AbstractSchemaTest.js +1 -0
- package/build/DynamicSchemaEntityImplementation.js +4 -3
- package/build/StaticSchemaEntityImpl.js +23 -13
- package/build/errors/SpruceError.js +3 -4
- package/build/errors/ValidateErrorMessageFormatter.js +11 -8
- package/build/esm/.spruce/schemas/fields/fieldClassMap.d.ts +2 -2
- package/build/esm/.spruce/schemas/fields/fieldClassMap.js +3 -3
- package/build/esm/StaticSchemaEntityImpl.js +6 -2
- package/build/esm/errors/ValidateErrorMessageFormatter.js +5 -1
- package/build/esm/fields/AddressField.d.ts +1 -1
- package/build/esm/fields/AddressField.js +3 -4
- package/build/esm/fields/BooleanField.d.ts +1 -1
- package/build/esm/fields/BooleanField.js +3 -4
- package/build/esm/fields/DateField.d.ts +1 -1
- package/build/esm/fields/DateField.js +3 -4
- package/build/esm/fields/DateTimeField.d.ts +1 -1
- package/build/esm/fields/DateTimeField.js +3 -4
- package/build/esm/fields/DirectoryField.d.ts +1 -1
- package/build/esm/fields/DirectoryField.js +9 -7
- package/build/esm/fields/DurationField.d.ts +1 -1
- package/build/esm/fields/DurationField.js +5 -5
- package/build/esm/fields/EmailField.d.ts +1 -1
- package/build/esm/fields/EmailField.js +3 -4
- package/build/esm/fields/FileField.d.ts +1 -1
- package/build/esm/fields/FileField.js +3 -4
- package/build/esm/fields/IdField.d.ts +1 -1
- package/build/esm/fields/IdField.js +3 -4
- package/build/esm/fields/ImageField.d.ts +1 -1
- package/build/esm/fields/ImageField.js +3 -4
- package/build/esm/fields/NumberField.d.ts +1 -1
- package/build/esm/fields/NumberField.js +3 -4
- package/build/esm/fields/PhoneField.d.ts +1 -1
- package/build/esm/fields/PhoneField.js +3 -4
- package/build/esm/fields/RawField.d.ts +1 -1
- package/build/esm/fields/RawField.js +3 -4
- package/build/esm/fields/SchemaField.d.ts +1 -1
- package/build/esm/fields/SchemaField.js +23 -13
- package/build/esm/fields/SchemaField.types.d.ts +2 -2
- package/build/esm/fields/SelectField.d.ts +1 -1
- package/build/esm/fields/SelectField.js +3 -4
- package/build/esm/fields/TextField.d.ts +1 -1
- package/build/esm/fields/TextField.js +7 -5
- package/build/esm/fields/field.static.types.d.ts +2 -4
- package/build/esm/schemas.static.types.d.ts +3 -10
- package/build/esm/tests/validationErrorAssert.utility.d.ts +2 -2
- package/build/esm/types/utilities.types.d.ts +4 -4
- package/build/esm/utilities/KeyMapper.js +0 -3
- package/build/esm/utilities/cloneDeep.js +2 -1
- package/build/esm/utilities/formatPhoneNumber.js +4 -2
- package/build/esm/utilities/mapFieldErrorsToParameterErrors.js +3 -1
- package/build/esm/utilities/registerFieldType.js +1 -3
- package/build/esm/utilities/validateSchemaValues.js +1 -3
- package/build/fields/AbstractField.js +7 -2
- package/build/fields/AddressField.d.ts +1 -1
- package/build/fields/AddressField.js +1 -3
- package/build/fields/BooleanField.d.ts +1 -1
- package/build/fields/BooleanField.js +1 -3
- package/build/fields/DateField.d.ts +1 -1
- package/build/fields/DateField.js +1 -3
- package/build/fields/DateTimeField.d.ts +1 -1
- package/build/fields/DateTimeField.js +2 -5
- package/build/fields/DirectoryField.d.ts +1 -1
- package/build/fields/DirectoryField.js +8 -7
- package/build/fields/DurationField.d.ts +1 -1
- package/build/fields/DurationField.js +4 -6
- package/build/fields/EmailField.d.ts +1 -1
- package/build/fields/EmailField.js +1 -3
- package/build/fields/FileField.d.ts +1 -1
- package/build/fields/FileField.js +3 -6
- package/build/fields/IdField.d.ts +1 -1
- package/build/fields/IdField.js +1 -3
- package/build/fields/ImageField.d.ts +1 -1
- package/build/fields/ImageField.js +3 -7
- package/build/fields/NumberField.d.ts +1 -1
- package/build/fields/NumberField.js +1 -3
- package/build/fields/PhoneField.d.ts +1 -1
- package/build/fields/PhoneField.js +1 -3
- package/build/fields/RawField.d.ts +1 -1
- package/build/fields/RawField.js +1 -3
- package/build/fields/SchemaField.d.ts +1 -1
- package/build/fields/SchemaField.js +28 -16
- package/build/fields/SchemaField.types.d.ts +2 -2
- package/build/fields/SelectField.d.ts +1 -1
- package/build/fields/SelectField.js +1 -3
- package/build/fields/TextField.d.ts +1 -1
- package/build/fields/TextField.js +6 -6
- package/build/fields/field.static.types.d.ts +2 -4
- package/build/schemas.static.types.d.ts +3 -10
- package/build/singletons/SchemaRegistry.js +4 -6
- package/build/tests/AbstractDateFieldTest.js +2 -0
- package/build/tests/validationErrorAssert.utility.d.ts +2 -2
- package/build/tests/validationErrorAssert.utility.js +2 -3
- package/build/types/utilities.types.d.ts +4 -4
- package/build/utilities/KeyMapper.js +3 -7
- package/build/utilities/areSchemasTheSame.js +2 -3
- package/build/utilities/assertOptions.js +1 -1
- package/build/utilities/cloneDeep.js +3 -3
- package/build/utilities/cloneDeepPreservingInstances.js +1 -2
- package/build/utilities/defaultSchemaValues.js +4 -1
- package/build/utilities/dropFields.js +3 -1
- package/build/utilities/dropPrivateFields.js +3 -1
- package/build/utilities/formatPhoneNumber.js +6 -5
- package/build/utilities/getFields.js +1 -2
- package/build/utilities/isSchemaValid.js +1 -1
- package/build/utilities/makeFieldsOptional.js +4 -1
- package/build/utilities/mapFieldErrorsToParameterErrors.js +15 -4
- package/build/utilities/normalizeFieldValue.js +13 -16
- package/build/utilities/normalizePartialSchemaValues.js +4 -1
- package/build/utilities/normalizeSchemaValues.js +5 -13
- package/build/utilities/registerFieldType.js +15 -6
- package/build/utilities/selectChoicesToHash.js +1 -2
- package/build/utilities/validateSchema.js +1 -2
- package/build/utilities/validateSchemaValues.js +2 -15
- package/package.json +12 -12
|
@@ -24,7 +24,6 @@ function flattenFields(fieldErrors, flattened, namePrefix = '') {
|
|
|
24
24
|
}
|
|
25
25
|
const validationErrorAssert = {
|
|
26
26
|
assertError(error, options) {
|
|
27
|
-
var _a, _b;
|
|
28
27
|
const missing = [];
|
|
29
28
|
const err = error;
|
|
30
29
|
if (!err) {
|
|
@@ -39,7 +38,7 @@ const validationErrorAssert = {
|
|
|
39
38
|
parameters: missing,
|
|
40
39
|
});
|
|
41
40
|
}
|
|
42
|
-
if (
|
|
41
|
+
if (err.options?.code !== 'VALIDATION_FAILED') {
|
|
43
42
|
throw new SpruceError_1.default({
|
|
44
43
|
code: 'INVALID_PARAMETERS',
|
|
45
44
|
parameters: ['error'],
|
|
@@ -62,7 +61,7 @@ const validationErrorAssert = {
|
|
|
62
61
|
for (let idx = 0; idx < keys.length; idx++) {
|
|
63
62
|
const code = codes[idx];
|
|
64
63
|
const key = keys[idx];
|
|
65
|
-
for (const lookup of
|
|
64
|
+
for (const lookup of options?.[key] ?? []) {
|
|
66
65
|
const match = flattened[lookup] === code;
|
|
67
66
|
if (!match) {
|
|
68
67
|
test_utils_1.assert.fail(buildFailMessage(code, lookup, flattened));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Schema, SchemaRequiredFieldNames } from '../schemas.static.types';
|
|
2
|
-
export type Unpack<A> = A extends
|
|
3
|
-
export type IsArray<T,
|
|
4
|
-
export type IsArrayNoUnpack<T,
|
|
5
|
-
export type IsRequired<T,
|
|
2
|
+
export type Unpack<A> = A extends (infer E)[] ? E : A;
|
|
3
|
+
export type IsArray<T, IsArray> = IsArray extends true ? Unpack<T>[] : Unpack<T>;
|
|
4
|
+
export type IsArrayNoUnpack<T, IsArray> = IsArray extends true ? T[] : T;
|
|
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;
|
|
@@ -5,16 +5,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
|
7
7
|
class KeyMapper {
|
|
8
|
+
map;
|
|
8
9
|
constructor(map) {
|
|
9
10
|
this.map = map;
|
|
10
11
|
}
|
|
11
12
|
mapTo(values, options) {
|
|
12
|
-
|
|
13
|
-
return this._mapTo(values, this.map, (_a = options === null || options === void 0 ? void 0 : options.shouldThrowOnUnmapped) !== null && _a !== void 0 ? _a : true);
|
|
13
|
+
return this._mapTo(values, this.map, options?.shouldThrowOnUnmapped ?? true);
|
|
14
14
|
}
|
|
15
15
|
mapFrom(values, options) {
|
|
16
|
-
|
|
17
|
-
return this._mapFrom(values, this.map, (_a = options === null || options === void 0 ? void 0 : options.shouldThrowOnUnmapped) !== null && _a !== void 0 ? _a : true);
|
|
16
|
+
return this._mapFrom(values, this.map, options?.shouldThrowOnUnmapped ?? true);
|
|
18
17
|
}
|
|
19
18
|
mapFieldNameTo(name) {
|
|
20
19
|
if (!this.map[name]) {
|
|
@@ -30,7 +29,6 @@ class KeyMapper {
|
|
|
30
29
|
}
|
|
31
30
|
mapFieldNameFrom(name) {
|
|
32
31
|
for (const key in this.map) {
|
|
33
|
-
// eslint-disable-next-line no-prototype-builtins
|
|
34
32
|
if (this.map.hasOwnProperty(key)) {
|
|
35
33
|
if (this.map[key] === name) {
|
|
36
34
|
return key;
|
|
@@ -44,7 +42,6 @@ class KeyMapper {
|
|
|
44
42
|
const foundFields = [];
|
|
45
43
|
let target = {};
|
|
46
44
|
for (const key in map) {
|
|
47
|
-
// eslint-disable-next-line no-prototype-builtins
|
|
48
45
|
if (values.hasOwnProperty(key)) {
|
|
49
46
|
target[map[key]] = values[key];
|
|
50
47
|
foundFields.push(key);
|
|
@@ -61,7 +58,6 @@ class KeyMapper {
|
|
|
61
58
|
let target = {};
|
|
62
59
|
for (const targetKey in map) {
|
|
63
60
|
const sourceKey = map[targetKey];
|
|
64
|
-
// eslint-disable-next-line no-prototype-builtins
|
|
65
61
|
if (values.hasOwnProperty(sourceKey)) {
|
|
66
62
|
target[targetKey] = values[sourceKey];
|
|
67
63
|
foundFields.push(sourceKey);
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
function areSchemasTheSame(left, right) {
|
|
4
|
-
var _a, _b;
|
|
5
4
|
if (left.id !== right.id) {
|
|
6
5
|
return false;
|
|
7
6
|
}
|
|
8
|
-
const fields1 = Object.keys(
|
|
9
|
-
const fields2 = Object.keys(
|
|
7
|
+
const fields1 = Object.keys(left.fields ?? {}).sort();
|
|
8
|
+
const fields2 = Object.keys(right.fields ?? {}).sort();
|
|
10
9
|
if (fields1.join('|') !== fields2.join('|')) {
|
|
11
10
|
return false;
|
|
12
11
|
}
|
|
@@ -8,7 +8,7 @@ const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
|
|
8
8
|
function assertOptions(options, toCheck, friendlyMessage) {
|
|
9
9
|
const missing = [];
|
|
10
10
|
for (const check of toCheck) {
|
|
11
|
-
const value = (0, just_safe_get_1.default)(options
|
|
11
|
+
const value = (0, just_safe_get_1.default)(options ?? {}, check);
|
|
12
12
|
//@ts-ignore
|
|
13
13
|
if (value === null || typeof value === 'undefined') {
|
|
14
14
|
missing.push(check);
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
function cloneDeep(obj, transformer) {
|
|
4
|
-
var _a;
|
|
5
4
|
const o = obj;
|
|
6
5
|
let result = o;
|
|
7
6
|
let type = {}.toString.call(o).slice(8, -1);
|
|
@@ -11,7 +10,7 @@ function cloneDeep(obj, transformer) {
|
|
|
11
10
|
if (type == 'Map') {
|
|
12
11
|
const items = [];
|
|
13
12
|
[...o.entries()].forEach((kv) => {
|
|
14
|
-
if (
|
|
13
|
+
if (transformer?.(kv[1], kv[0]) !== false) {
|
|
15
14
|
items.push([cloneDeep(kv[0]), cloneDeep(kv[1])]);
|
|
16
15
|
}
|
|
17
16
|
});
|
|
@@ -26,7 +25,8 @@ function cloneDeep(obj, transformer) {
|
|
|
26
25
|
if (type == 'Array' || type == 'Object') {
|
|
27
26
|
result = Array.isArray(o) ? [] : {};
|
|
28
27
|
for (let key in o) {
|
|
29
|
-
result[key] =
|
|
28
|
+
result[key] =
|
|
29
|
+
transformer?.(o[key], key) ?? cloneDeep(o[key], transformer);
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
// primitives and non-supported objects (e.g. functions) land here
|
|
@@ -6,8 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const cloneDeep_1 = __importDefault(require("./cloneDeep"));
|
|
7
7
|
function cloneDeepPreservingInstances(v) {
|
|
8
8
|
return (0, cloneDeep_1.default)(v, (value) => {
|
|
9
|
-
|
|
10
|
-
const name = (_b = (_a = value === null || value === void 0 ? void 0 : value.__proto__) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name;
|
|
9
|
+
const name = value?.__proto__?.constructor?.name;
|
|
11
10
|
if (name && name !== 'Object') {
|
|
12
11
|
return value;
|
|
13
12
|
}
|
|
@@ -7,6 +7,9 @@ const StaticSchemaEntityImpl_1 = __importDefault(require("../StaticSchemaEntityI
|
|
|
7
7
|
function defaultSchemaValues(definition, options = {}) {
|
|
8
8
|
const instance = new StaticSchemaEntityImpl_1.default(definition);
|
|
9
9
|
// @ts-ignore
|
|
10
|
-
return instance.getDefaultValues(
|
|
10
|
+
return instance.getDefaultValues({
|
|
11
|
+
shouldCreateEntityInstances: false,
|
|
12
|
+
...options,
|
|
13
|
+
});
|
|
11
14
|
}
|
|
12
15
|
exports.default = defaultSchemaValues;
|
|
@@ -4,7 +4,9 @@ function dropFields(fields, dropFields) {
|
|
|
4
4
|
const optionalFields = {};
|
|
5
5
|
Object.keys(fields).forEach((name) => {
|
|
6
6
|
if (!dropFields || dropFields.indexOf(name) === -1) {
|
|
7
|
-
optionalFields[name] =
|
|
7
|
+
optionalFields[name] = {
|
|
8
|
+
...fields[name],
|
|
9
|
+
};
|
|
8
10
|
}
|
|
9
11
|
});
|
|
10
12
|
return optionalFields;
|
|
@@ -4,7 +4,9 @@ function dropPrivateFields(fields) {
|
|
|
4
4
|
const optionalFields = {};
|
|
5
5
|
Object.keys(fields).forEach((name) => {
|
|
6
6
|
if (!fields[name].isPrivate) {
|
|
7
|
-
optionalFields[name] =
|
|
7
|
+
optionalFields[name] = {
|
|
8
|
+
...fields[name],
|
|
9
|
+
};
|
|
8
10
|
}
|
|
9
11
|
});
|
|
10
12
|
return optionalFields;
|
|
@@ -28,15 +28,14 @@ function formatNumberWithCode(phoneNumberString, code = '1') {
|
|
|
28
28
|
return null;
|
|
29
29
|
}
|
|
30
30
|
function isValidNumber(number) {
|
|
31
|
-
var _a;
|
|
32
31
|
const code = getCode(number);
|
|
33
|
-
const formatted =
|
|
34
|
-
return
|
|
32
|
+
const formatted = formatNumberWithCode(number, code)?.replace(/[^0-9]/g, '');
|
|
33
|
+
return formatted?.length === 11 || formatted?.length === 12;
|
|
35
34
|
}
|
|
36
35
|
exports.isValidNumber = isValidNumber;
|
|
37
36
|
function getCode(number) {
|
|
38
37
|
let code = `1`; // Default to North American country code
|
|
39
|
-
const cleaned = number.replace(
|
|
38
|
+
const cleaned = number.replace(/(?!^\+)[^\d]/g, '');
|
|
40
39
|
// Explicitly check for '+' sign to distinguish international codes
|
|
41
40
|
if (cleaned.startsWith('+90') ||
|
|
42
41
|
(cleaned.startsWith('90') && cleaned.length > 10)) {
|
|
@@ -63,7 +62,9 @@ function formatPhoneNumber(val, shouldFailSilently = true) {
|
|
|
63
62
|
return val;
|
|
64
63
|
}
|
|
65
64
|
}
|
|
66
|
-
|
|
65
|
+
// remove trailing spaces
|
|
66
|
+
const cleaned = formatted.replace(/\s+$/, '');
|
|
67
|
+
return cleaned;
|
|
67
68
|
}
|
|
68
69
|
exports.default = formatPhoneNumber;
|
|
69
70
|
function isDummyNumber(phone) {
|
|
@@ -5,8 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
|
7
7
|
function getFields(schema) {
|
|
8
|
-
|
|
9
|
-
const names = Object.keys((_a = schema === null || schema === void 0 ? void 0 : schema.fields) !== null && _a !== void 0 ? _a : {});
|
|
8
|
+
const names = Object.keys(schema?.fields ?? {});
|
|
10
9
|
if (names.length === 0) {
|
|
11
10
|
throw new SpruceError_1.default({
|
|
12
11
|
code: 'INVALID_PARAMETERS',
|
|
@@ -3,7 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
function makeFieldsOptional(fields) {
|
|
4
4
|
const optionalFields = {};
|
|
5
5
|
Object.keys(fields).forEach((name) => {
|
|
6
|
-
optionalFields[name] =
|
|
6
|
+
optionalFields[name] = {
|
|
7
|
+
...fields[name],
|
|
8
|
+
isRequired: false,
|
|
9
|
+
};
|
|
7
10
|
});
|
|
8
11
|
return optionalFields;
|
|
9
12
|
}
|
|
@@ -36,7 +36,9 @@ function pullParamaterIssues(errors, prefix = '') {
|
|
|
36
36
|
const invalidParameters = [];
|
|
37
37
|
const unexpectedParamaters = [];
|
|
38
38
|
errors.forEach((fieldError) => {
|
|
39
|
-
const fieldName = prefix
|
|
39
|
+
const fieldName = prefix
|
|
40
|
+
? `${prefix}.${fieldError.name}`
|
|
41
|
+
: fieldError.name;
|
|
40
42
|
if (fieldError.errors) {
|
|
41
43
|
const { missingParameters: m, invalidParameters: i, unexpectedParamaters: u, } = pullParamaterIssues(fieldError.errors, fieldName);
|
|
42
44
|
missingParameters.push(...m);
|
|
@@ -44,13 +46,22 @@ function pullParamaterIssues(errors, prefix = '') {
|
|
|
44
46
|
unexpectedParamaters.push(...u);
|
|
45
47
|
}
|
|
46
48
|
else if (fieldError.code === 'MISSING_PARAMETER') {
|
|
47
|
-
missingParameters.push(
|
|
49
|
+
missingParameters.push({
|
|
50
|
+
...fieldError,
|
|
51
|
+
name: fieldName,
|
|
52
|
+
});
|
|
48
53
|
}
|
|
49
54
|
else if (fieldError.code === 'INVALID_PARAMETER') {
|
|
50
|
-
invalidParameters.push(
|
|
55
|
+
invalidParameters.push({
|
|
56
|
+
...fieldError,
|
|
57
|
+
name: fieldName,
|
|
58
|
+
});
|
|
51
59
|
}
|
|
52
60
|
else if (fieldError.code === 'UNEXPECTED_PARAMETER') {
|
|
53
|
-
unexpectedParamaters.push(
|
|
61
|
+
unexpectedParamaters.push({
|
|
62
|
+
...fieldError,
|
|
63
|
+
name: fieldName,
|
|
64
|
+
});
|
|
54
65
|
}
|
|
55
66
|
});
|
|
56
67
|
return { missingParameters, invalidParameters, unexpectedParamaters };
|
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
-
var t = {};
|
|
4
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
-
t[p] = s[p];
|
|
6
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
-
t[p[i]] = s[p[i]];
|
|
10
|
-
}
|
|
11
|
-
return t;
|
|
12
|
-
};
|
|
13
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
4
|
};
|
|
@@ -17,7 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
6
|
exports.normalizeValueToArray = void 0;
|
|
18
7
|
const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
|
19
8
|
function normalizeFieldValue(schemaId, schemaName, schemasById, field, value, options) {
|
|
20
|
-
var _a;
|
|
21
9
|
let localValue = normalizeValueToArray(value);
|
|
22
10
|
if (!Array.isArray(localValue)) {
|
|
23
11
|
throw new SpruceError_1.default({
|
|
@@ -26,8 +14,12 @@ function normalizeFieldValue(schemaId, schemaName, schemasById, field, value, op
|
|
|
26
14
|
friendlyMessages: [`I was expecting an array for ${field.name}.`],
|
|
27
15
|
});
|
|
28
16
|
}
|
|
29
|
-
const
|
|
30
|
-
const baseOptions =
|
|
17
|
+
const { shouldValidate: validate = true, shouldCreateEntityInstances: createEntityInstances = true, ...extraOptions } = options ?? {};
|
|
18
|
+
const baseOptions = {
|
|
19
|
+
schemasById,
|
|
20
|
+
...(field.definition.options ?? {}),
|
|
21
|
+
...extraOptions,
|
|
22
|
+
};
|
|
31
23
|
if (value === null || typeof value === 'undefined') {
|
|
32
24
|
if (field && (!validate || !field.isRequired)) {
|
|
33
25
|
return value;
|
|
@@ -45,7 +37,9 @@ function normalizeFieldValue(schemaId, schemaName, schemasById, field, value, op
|
|
|
45
37
|
localValue.forEach((value) => {
|
|
46
38
|
errors = [
|
|
47
39
|
...errors,
|
|
48
|
-
...field.validate(value,
|
|
40
|
+
...field.validate(value, {
|
|
41
|
+
...baseOptions,
|
|
42
|
+
}),
|
|
49
43
|
];
|
|
50
44
|
});
|
|
51
45
|
}
|
|
@@ -60,7 +54,10 @@ function normalizeFieldValue(schemaId, schemaName, schemasById, field, value, op
|
|
|
60
54
|
if (localValue.length > 0) {
|
|
61
55
|
localValue = localValue.map((value) => typeof value === 'undefined'
|
|
62
56
|
? undefined
|
|
63
|
-
: field.toValueType(value,
|
|
57
|
+
: field.toValueType(value, {
|
|
58
|
+
createEntityInstances,
|
|
59
|
+
...baseOptions,
|
|
60
|
+
}));
|
|
64
61
|
}
|
|
65
62
|
return (field.isArray ? localValue : localValue[0]);
|
|
66
63
|
}
|
|
@@ -5,7 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const normalizeSchemaValues_1 = __importDefault(require("./normalizeSchemaValues"));
|
|
7
7
|
function normalizePartialSchemaValues(schema, values, options) {
|
|
8
|
-
const normalized = (0, normalizeSchemaValues_1.default)(schema, values,
|
|
8
|
+
const normalized = (0, normalizeSchemaValues_1.default)(schema, values, {
|
|
9
|
+
...options,
|
|
10
|
+
shouldIncludeNullAndUndefinedFields: false,
|
|
11
|
+
});
|
|
9
12
|
return normalized;
|
|
10
13
|
}
|
|
11
14
|
exports.default = normalizePartialSchemaValues;
|
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
-
var t = {};
|
|
4
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
-
t[p] = s[p];
|
|
6
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
-
t[p[i]] = s[p[i]];
|
|
10
|
-
}
|
|
11
|
-
return t;
|
|
12
|
-
};
|
|
13
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
4
|
};
|
|
@@ -17,8 +6,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
6
|
const SchemaEntityFactory_1 = __importDefault(require("../factories/SchemaEntityFactory"));
|
|
18
7
|
function normalizeSchemaValues(schema, values, options) {
|
|
19
8
|
const instance = SchemaEntityFactory_1.default.Entity(schema, values);
|
|
20
|
-
const
|
|
21
|
-
const normalizedOptions =
|
|
9
|
+
const { shouldCreateEntityInstances = false, ...rest } = options || {};
|
|
10
|
+
const normalizedOptions = {
|
|
11
|
+
shouldCreateEntityInstances,
|
|
12
|
+
...rest,
|
|
13
|
+
};
|
|
22
14
|
return instance.getValues(normalizedOptions);
|
|
23
15
|
}
|
|
24
16
|
exports.default = normalizeSchemaValues;
|
|
@@ -5,9 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.validateFieldRegistration = void 0;
|
|
7
7
|
const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
|
8
|
-
function validateFieldRegistration(registration
|
|
9
|
-
// eslint-disable-next-line no-undef
|
|
10
|
-
) {
|
|
8
|
+
function validateFieldRegistration(registration) {
|
|
11
9
|
const errors = [];
|
|
12
10
|
const builtRegistration = {
|
|
13
11
|
package: '***missing***',
|
|
@@ -32,16 +30,27 @@ function validateFieldRegistration(registration
|
|
|
32
30
|
});
|
|
33
31
|
}
|
|
34
32
|
if (errors.length > 0) {
|
|
35
|
-
throw new SpruceError_1.default(
|
|
33
|
+
throw new SpruceError_1.default({
|
|
34
|
+
code: 'INVALID_FIELD_REGISTRATION',
|
|
35
|
+
...builtRegistration,
|
|
36
|
+
});
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
exports.validateFieldRegistration = validateFieldRegistration;
|
|
39
40
|
/** Register a new type of field */
|
|
40
41
|
function registerFieldType(options) {
|
|
41
|
-
const registration =
|
|
42
|
+
const registration = {
|
|
43
|
+
package: options.package,
|
|
44
|
+
className: options.class.name,
|
|
45
|
+
type: options.type,
|
|
46
|
+
importAs: options.importAs,
|
|
47
|
+
// TODO change this up when typescript supports typing static methods on a class
|
|
48
|
+
// @ts-ignore
|
|
49
|
+
description: options.class.description,
|
|
42
50
|
// TODO change this up when typescript supports typing static methods on a class
|
|
43
51
|
// @ts-ignore
|
|
44
|
-
|
|
52
|
+
...options.class.generateTypeDetails(),
|
|
53
|
+
};
|
|
45
54
|
validateFieldRegistration(registration);
|
|
46
55
|
return registration;
|
|
47
56
|
}
|
|
@@ -13,10 +13,9 @@ function selectChoicesToHash(options) {
|
|
|
13
13
|
exports.selectChoicesToHash = selectChoicesToHash;
|
|
14
14
|
/** Take a definition and a field name and returns a value/label hash */
|
|
15
15
|
function schemaChoicesToHash(definition, fieldName) {
|
|
16
|
-
var _a, _b, _c, _d;
|
|
17
16
|
// @ts-ignore
|
|
18
17
|
return selectChoicesToHash(
|
|
19
18
|
// @ts-ignore
|
|
20
|
-
|
|
19
|
+
definition.fields?.[fieldName]?.options?.choices ?? []);
|
|
21
20
|
}
|
|
22
21
|
exports.schemaChoicesToHash = schemaChoicesToHash;
|
|
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
|
7
7
|
function validateSchema(schema) {
|
|
8
|
-
var _a;
|
|
9
8
|
const errors = [];
|
|
10
9
|
if (!schema) {
|
|
11
10
|
errors.push('definition_empty');
|
|
@@ -27,7 +26,7 @@ function validateSchema(schema) {
|
|
|
27
26
|
if (errors.length > 0) {
|
|
28
27
|
throw new SpruceError_1.default({
|
|
29
28
|
code: 'INVALID_SCHEMA',
|
|
30
|
-
schemaId:
|
|
29
|
+
schemaId: schema?.id ?? 'ID MISSING',
|
|
31
30
|
errors,
|
|
32
31
|
});
|
|
33
32
|
}
|
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
-
var t = {};
|
|
4
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
-
t[p] = s[p];
|
|
6
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
-
t[p[i]] = s[p[i]];
|
|
10
|
-
}
|
|
11
|
-
return t;
|
|
12
|
-
};
|
|
13
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
4
|
};
|
|
@@ -17,10 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
6
|
const just_safe_set_1 = __importDefault(require("just-safe-set"));
|
|
18
7
|
const __1 = require("..");
|
|
19
8
|
const SchemaEntityFactory_1 = __importDefault(require("../factories/SchemaEntityFactory"));
|
|
20
|
-
function validateSchemaValues(schema, values, options
|
|
21
|
-
|
|
22
|
-
) {
|
|
23
|
-
const opts = __rest(options !== null && options !== void 0 ? options : {}, []);
|
|
9
|
+
function validateSchemaValues(schema, values, options) {
|
|
10
|
+
const { ...opts } = options ?? {};
|
|
24
11
|
(0, __1.validateSchema)(schema);
|
|
25
12
|
const mapped = Object.keys(values).reduce((mapped, key) => {
|
|
26
13
|
//@ts-ignore
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"!build/__tests__",
|
|
9
9
|
"esm"
|
|
10
10
|
],
|
|
11
|
-
"version": "
|
|
11
|
+
"version": "30.0.0",
|
|
12
12
|
"main": "./build/index.js",
|
|
13
13
|
"types": "./build/index.d.ts",
|
|
14
14
|
"module": "./build/esm/index.js",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"clean.build": "rm -rf build/",
|
|
44
44
|
"clean.dependencies": "rm -rf node_modules/ package-lock.json yarn.lock",
|
|
45
45
|
"clean.dist": "rm -rf build/__tests__ build/esm/__tests__",
|
|
46
|
-
"fix.lint": "eslint --fix --cache
|
|
47
|
-
"lint": "eslint --cache
|
|
46
|
+
"fix.lint": "eslint --fix --cache",
|
|
47
|
+
"lint": "eslint --cache",
|
|
48
48
|
"lint.tsc": "tsc -p . --noEmit",
|
|
49
49
|
"rebuild": "yarn clean.all && yarn && yarn build.dev",
|
|
50
50
|
"release": "semantic-release",
|
|
@@ -62,30 +62,30 @@
|
|
|
62
62
|
"build.copy-files": "mkdir -p build && rsync -avzq --exclude='*.ts' ./src/ ./build/"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@sprucelabs/error": "^
|
|
66
|
-
"@sprucelabs/test-utils": "^
|
|
65
|
+
"@sprucelabs/error": "^6.0.1",
|
|
66
|
+
"@sprucelabs/test-utils": "^5.0.0",
|
|
67
67
|
"email-validator": "^2.0.4",
|
|
68
68
|
"just-safe-get": "^4.2.0",
|
|
69
69
|
"just-safe-set": "^4.2.1"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"@sprucelabs/esm-postbuild": "^
|
|
73
|
-
"@sprucelabs/jest-json-reporter": "^7.0.
|
|
72
|
+
"@sprucelabs/esm-postbuild": "^6.0.0",
|
|
73
|
+
"@sprucelabs/jest-json-reporter": "^7.0.207",
|
|
74
74
|
"@sprucelabs/jest-sheets-reporter": "^3.0.26",
|
|
75
75
|
"@sprucelabs/resolve-path-aliases": "^1.1.304",
|
|
76
|
-
"@sprucelabs/semantic-release": "^
|
|
77
|
-
"@sprucelabs/test": "^
|
|
76
|
+
"@sprucelabs/semantic-release": "^5.0.1",
|
|
77
|
+
"@sprucelabs/test": "^9.0.2",
|
|
78
78
|
"chokidar-cli": "^3.0.0",
|
|
79
79
|
"concurrently": "^8.2.2",
|
|
80
|
-
"eslint": "^
|
|
81
|
-
"eslint-config-spruce": "^
|
|
80
|
+
"eslint": "^9.0.0",
|
|
81
|
+
"eslint-config-spruce": "^11.2.3",
|
|
82
82
|
"jest": "^29.7.0",
|
|
83
83
|
"jest-circus": "^29.7.0",
|
|
84
84
|
"prettier": "^3.2.5",
|
|
85
85
|
"ts-node": "^10.9.2",
|
|
86
86
|
"tsc-watch": "^6.2.0",
|
|
87
87
|
"tsconfig-paths": "^4.2.0",
|
|
88
|
-
"typescript": "^5.4.
|
|
88
|
+
"typescript": "^5.4.5"
|
|
89
89
|
},
|
|
90
90
|
"jest": {
|
|
91
91
|
"testEnvironment": "node",
|