@sprucelabs/schema 33.2.9 → 34.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.
|
@@ -123,19 +123,26 @@ class StaticSchemaEntityImpl extends AbstractEntity_1.default {
|
|
|
123
123
|
}
|
|
124
124
|
this.getNamedFields(options).forEach((namedField) => {
|
|
125
125
|
const { name, field } = namedField;
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
const value = this.values[name];
|
|
127
|
+
const wasArray = Array.isArray(value);
|
|
128
|
+
let valueAsArray = (0, normalizeFieldValue_1.normalizeValueToArray)(value);
|
|
129
|
+
const isMissingRequiredOrMinValues = field.isRequired &&
|
|
128
130
|
field.isArray &&
|
|
129
|
-
(!
|
|
130
|
-
|
|
131
|
+
(!value || valueAsArray.length < (field.minArrayLength ?? 1));
|
|
132
|
+
const isMissing = !value;
|
|
133
|
+
const shouldBeArrayButIsnt = !wasArray && !isMissing && field.isArray;
|
|
134
|
+
if (isMissingRequiredOrMinValues || shouldBeArrayButIsnt) {
|
|
135
|
+
const missingRequiredError = `${field.label ? `'${field.label}'` : 'This'} is required!`;
|
|
136
|
+
const missingMinValuesError = `${field.label ? `'${field.label}'` : 'You'} must ${field.label ? 'have' : 'select'} at least ${field.minArrayLength} value${field.minArrayLength === 1 ? '' : 's'}. I found ${valueAsArray.length}!`;
|
|
137
|
+
const mustBeArrayError = `${field.label ? `'${field.label}'` : 'This'} must be an array!`;
|
|
131
138
|
errors.push({
|
|
132
|
-
code:
|
|
133
|
-
? 'MISSING_PARAMETER'
|
|
134
|
-
: 'INVALID_PARAMETER',
|
|
139
|
+
code: isMissing ? 'MISSING_PARAMETER' : 'INVALID_PARAMETER',
|
|
135
140
|
name,
|
|
136
|
-
friendlyMessage:
|
|
137
|
-
?
|
|
138
|
-
:
|
|
141
|
+
friendlyMessage: isMissing
|
|
142
|
+
? missingRequiredError
|
|
143
|
+
: shouldBeArrayButIsnt
|
|
144
|
+
? mustBeArrayError
|
|
145
|
+
: missingMinValuesError,
|
|
139
146
|
});
|
|
140
147
|
}
|
|
141
148
|
else {
|
|
@@ -87,19 +87,26 @@ class StaticSchemaEntityImpl extends AbstractEntity {
|
|
|
87
87
|
this.getNamedFields(options).forEach((namedField) => {
|
|
88
88
|
var _a, _b;
|
|
89
89
|
const { name, field } = namedField;
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
const value = this.values[name];
|
|
91
|
+
const wasArray = Array.isArray(value);
|
|
92
|
+
let valueAsArray = normalizeValueToArray(value);
|
|
93
|
+
const isMissingRequiredOrMinValues = field.isRequired &&
|
|
92
94
|
field.isArray &&
|
|
93
|
-
(!
|
|
94
|
-
|
|
95
|
+
(!value || valueAsArray.length < ((_a = field.minArrayLength) !== null && _a !== void 0 ? _a : 1));
|
|
96
|
+
const isMissing = !value;
|
|
97
|
+
const shouldBeArrayButIsnt = !wasArray && !isMissing && field.isArray;
|
|
98
|
+
if (isMissingRequiredOrMinValues || shouldBeArrayButIsnt) {
|
|
99
|
+
const missingRequiredError = `${field.label ? `'${field.label}'` : 'This'} is required!`;
|
|
100
|
+
const missingMinValuesError = `${field.label ? `'${field.label}'` : 'You'} must ${field.label ? 'have' : 'select'} at least ${field.minArrayLength} value${field.minArrayLength === 1 ? '' : 's'}. I found ${valueAsArray.length}!`;
|
|
101
|
+
const mustBeArrayError = `${field.label ? `'${field.label}'` : 'This'} must be an array!`;
|
|
95
102
|
errors.push({
|
|
96
|
-
code:
|
|
97
|
-
? 'MISSING_PARAMETER'
|
|
98
|
-
: 'INVALID_PARAMETER',
|
|
103
|
+
code: isMissing ? 'MISSING_PARAMETER' : 'INVALID_PARAMETER',
|
|
99
104
|
name,
|
|
100
|
-
friendlyMessage:
|
|
101
|
-
?
|
|
102
|
-
:
|
|
105
|
+
friendlyMessage: isMissing
|
|
106
|
+
? missingRequiredError
|
|
107
|
+
: shouldBeArrayButIsnt
|
|
108
|
+
? mustBeArrayError
|
|
109
|
+
: missingMinValuesError,
|
|
103
110
|
});
|
|
104
111
|
}
|
|
105
112
|
else {
|