@aeriajs/validation 0.0.183 → 0.0.185
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/dist/validate.js +48 -31
- package/package.json +6 -6
package/dist/validate.js
CHANGED
|
@@ -29,7 +29,7 @@ const getPropertyType = (property) => {
|
|
|
29
29
|
return 'object';
|
|
30
30
|
}
|
|
31
31
|
};
|
|
32
|
-
const
|
|
32
|
+
const makePropertyValidationError = (type, details) => {
|
|
33
33
|
return {
|
|
34
34
|
type,
|
|
35
35
|
details,
|
|
@@ -49,7 +49,7 @@ export const validateProperty = (what, property, options = {}) => {
|
|
|
49
49
|
if (options.tolerateExtraneous) {
|
|
50
50
|
return Result.result(undefined);
|
|
51
51
|
}
|
|
52
|
-
return Result.error(
|
|
52
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.Extraneous));
|
|
53
53
|
}
|
|
54
54
|
if (what === null || what === undefined) {
|
|
55
55
|
return Result.result(what);
|
|
@@ -66,19 +66,6 @@ export const validateProperty = (what, property, options = {}) => {
|
|
|
66
66
|
if (expectedType === 'datetime' && what instanceof Date) {
|
|
67
67
|
return Result.result(what);
|
|
68
68
|
}
|
|
69
|
-
if ('$ref' in property) {
|
|
70
|
-
switch (typeof what) {
|
|
71
|
-
case 'string': {
|
|
72
|
-
if (isValidObjectId(what)) {
|
|
73
|
-
return Result.result(what);
|
|
74
|
-
}
|
|
75
|
-
return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
|
|
76
|
-
expected: expectedType,
|
|
77
|
-
got: actualType,
|
|
78
|
-
}));
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
69
|
if (options.coerce) {
|
|
83
70
|
if (expectedType === 'number' && typeof what === 'string') {
|
|
84
71
|
const coerced = parseFloat(what);
|
|
@@ -104,9 +91,24 @@ export const validateProperty = (what, property, options = {}) => {
|
|
|
104
91
|
return Result.result(new options.objectIdConstructor(what));
|
|
105
92
|
}
|
|
106
93
|
}
|
|
107
|
-
|
|
94
|
+
if ('$ref' in property) {
|
|
95
|
+
switch (typeof what) {
|
|
96
|
+
case 'string': {
|
|
97
|
+
if (!isValidObjectId(what)) {
|
|
98
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.Unmatching, {
|
|
99
|
+
expected: expectedType,
|
|
100
|
+
got: actualType,
|
|
101
|
+
message: property.validationMessage,
|
|
102
|
+
}));
|
|
103
|
+
}
|
|
104
|
+
return Result.result(what);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.Unmatching, {
|
|
108
109
|
expected: expectedType,
|
|
109
110
|
got: actualType,
|
|
111
|
+
message: property.validationMessage,
|
|
110
112
|
}));
|
|
111
113
|
}
|
|
112
114
|
if ('type' in property) {
|
|
@@ -118,25 +120,28 @@ export const validateProperty = (what, property, options = {}) => {
|
|
|
118
120
|
throw new Error;
|
|
119
121
|
}
|
|
120
122
|
if (!(what instanceof options.objectIdConstructor)) {
|
|
121
|
-
return Result.error(
|
|
123
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.StringConstraint, {
|
|
122
124
|
expected: 'objectid',
|
|
123
125
|
got: 'invalid_objectid',
|
|
126
|
+
message: property.validationMessage,
|
|
124
127
|
}));
|
|
125
128
|
}
|
|
126
129
|
break;
|
|
127
130
|
}
|
|
128
131
|
default: {
|
|
129
132
|
if (typeof what !== 'string') {
|
|
130
|
-
return Result.error(
|
|
133
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.Unmatching, {
|
|
131
134
|
expected: expectedType,
|
|
132
135
|
got: actualType,
|
|
136
|
+
message: property.validationMessage,
|
|
133
137
|
}));
|
|
134
138
|
}
|
|
135
139
|
if ((typeof property.minLength === 'number' && property.minLength > what.length)
|
|
136
140
|
|| (typeof property.maxLength === 'number' && property.maxLength < what.length)) {
|
|
137
|
-
return Result.error(
|
|
141
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.StringConstraint, {
|
|
138
142
|
expected: 'string',
|
|
139
143
|
got: 'invalid_string',
|
|
144
|
+
message: property.validationMessage,
|
|
140
145
|
}));
|
|
141
146
|
}
|
|
142
147
|
}
|
|
@@ -145,26 +150,29 @@ export const validateProperty = (what, property, options = {}) => {
|
|
|
145
150
|
}
|
|
146
151
|
case 'integer': {
|
|
147
152
|
if (!Number.isInteger(what)) {
|
|
148
|
-
return Result.error(
|
|
153
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.NumericConstraint, {
|
|
149
154
|
expected: 'integer',
|
|
150
155
|
got: 'invalid_number',
|
|
156
|
+
message: property.validationMessage,
|
|
151
157
|
}));
|
|
152
158
|
}
|
|
153
159
|
}
|
|
154
160
|
case 'number': {
|
|
155
161
|
if (typeof what !== 'number') {
|
|
156
|
-
return Result.error(
|
|
162
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.Unmatching, {
|
|
157
163
|
expected: expectedType,
|
|
158
164
|
got: actualType,
|
|
165
|
+
message: property.validationMessage,
|
|
159
166
|
}));
|
|
160
167
|
}
|
|
161
168
|
if ((typeof property.maximum === 'number' && property.maximum < what)
|
|
162
169
|
|| (typeof property.minimum === 'number' && property.minimum > what)
|
|
163
170
|
|| (typeof property.exclusiveMaximum === 'number' && property.exclusiveMaximum <= what)
|
|
164
171
|
|| (typeof property.exclusiveMinimum === 'number' && property.exclusiveMinimum >= what)) {
|
|
165
|
-
return Result.error(
|
|
172
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.NumericConstraint, {
|
|
166
173
|
expected: 'number',
|
|
167
174
|
got: 'invalid_number',
|
|
175
|
+
message: property.validationMessage,
|
|
168
176
|
}));
|
|
169
177
|
}
|
|
170
178
|
break;
|
|
@@ -180,24 +188,26 @@ export const validateProperty = (what, property, options = {}) => {
|
|
|
180
188
|
}
|
|
181
189
|
case 'array': {
|
|
182
190
|
if (!Array.isArray(what)) {
|
|
183
|
-
return Result.error(
|
|
191
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.Unmatching, {
|
|
184
192
|
expected: expectedType,
|
|
185
193
|
got: actualType,
|
|
194
|
+
message: property.validationMessage,
|
|
186
195
|
}));
|
|
187
196
|
}
|
|
188
197
|
if (property.minItems) {
|
|
189
198
|
if (what.length < property.minItems) {
|
|
190
|
-
return Result.error(
|
|
199
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.MoreItemsExpected));
|
|
191
200
|
}
|
|
192
201
|
}
|
|
193
202
|
if (property.maxItems) {
|
|
194
203
|
if (what.length > property.maxItems) {
|
|
195
|
-
return Result.error(
|
|
204
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.LessItemsExpected));
|
|
196
205
|
}
|
|
197
206
|
}
|
|
198
207
|
let i = 0;
|
|
208
|
+
const validatedArray = [];
|
|
199
209
|
for (const elem of what) {
|
|
200
|
-
const { error } = validateProperty(elem, property.items, options);
|
|
210
|
+
const { error, result: validated } = validateProperty(elem, property.items, options);
|
|
201
211
|
if (error) {
|
|
202
212
|
if ('code' in error) {
|
|
203
213
|
continue;
|
|
@@ -206,23 +216,27 @@ export const validateProperty = (what, property, options = {}) => {
|
|
|
206
216
|
return Result.error(error);
|
|
207
217
|
}
|
|
208
218
|
i++;
|
|
219
|
+
validatedArray.push(validated);
|
|
209
220
|
}
|
|
221
|
+
return Result.result(validatedArray);
|
|
210
222
|
}
|
|
211
223
|
}
|
|
212
224
|
}
|
|
213
225
|
else if ('enum' in property) {
|
|
214
226
|
if (!property.enum.includes(what)) {
|
|
215
|
-
return Result.error(
|
|
227
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.ExtraneousElement, {
|
|
216
228
|
expected: property.enum,
|
|
217
229
|
got: what,
|
|
230
|
+
message: property.validationMessage,
|
|
218
231
|
}));
|
|
219
232
|
}
|
|
220
233
|
}
|
|
221
234
|
else if ('const' in property) {
|
|
222
235
|
if (what !== property.const) {
|
|
223
|
-
return Result.error(
|
|
236
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.Unmatching, {
|
|
224
237
|
expected: property.const,
|
|
225
238
|
got: what,
|
|
239
|
+
message: property.validationMessage,
|
|
226
240
|
}));
|
|
227
241
|
}
|
|
228
242
|
}
|
|
@@ -238,9 +252,10 @@ export const validateRefs = async (what, property, options = {}) => {
|
|
|
238
252
|
return Result.result({});
|
|
239
253
|
}
|
|
240
254
|
if (!isValidObjectId(String(what))) {
|
|
241
|
-
return Result.error(
|
|
255
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.Unmatching, {
|
|
242
256
|
expected: 'objectid',
|
|
243
257
|
got: typeof what,
|
|
258
|
+
message: property.validationMessage,
|
|
244
259
|
}));
|
|
245
260
|
}
|
|
246
261
|
let query;
|
|
@@ -265,9 +280,10 @@ export const validateRefs = async (what, property, options = {}) => {
|
|
|
265
280
|
},
|
|
266
281
|
});
|
|
267
282
|
if (!exists) {
|
|
268
|
-
return Result.error(
|
|
283
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.ReferenceConstraint, {
|
|
269
284
|
expected: 'objectid',
|
|
270
285
|
got: 'invalid_objectid',
|
|
286
|
+
message: property.validationMessage,
|
|
271
287
|
}));
|
|
272
288
|
}
|
|
273
289
|
return Result.result({});
|
|
@@ -284,9 +300,10 @@ export const validateRefs = async (what, property, options = {}) => {
|
|
|
284
300
|
description = collection.description;
|
|
285
301
|
}
|
|
286
302
|
if (typeof what !== 'object') {
|
|
287
|
-
return Result.error(
|
|
303
|
+
return Result.error(makePropertyValidationError(PropertyValidationErrorCode.Unmatching, {
|
|
288
304
|
expected: 'object',
|
|
289
305
|
got: typeof what,
|
|
306
|
+
message: property.validationMessage,
|
|
290
307
|
}));
|
|
291
308
|
}
|
|
292
309
|
return validate(what, description, options);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/validation",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.185",
|
|
5
5
|
"description": "## Installation",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -24,13 +24,13 @@
|
|
|
24
24
|
"@aeriajs/common": "link:../common",
|
|
25
25
|
"@aeriajs/entrypoint": "link:../entrypoint",
|
|
26
26
|
"@aeriajs/types": "link:../types",
|
|
27
|
-
"mongodb": "^6.
|
|
27
|
+
"mongodb": "^6.18.0"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"@aeriajs/common": "^0.0.
|
|
31
|
-
"@aeriajs/entrypoint": "^0.0.
|
|
32
|
-
"@aeriajs/types": "^0.0.
|
|
33
|
-
"mongodb": "^6.
|
|
30
|
+
"@aeriajs/common": "^0.0.161",
|
|
31
|
+
"@aeriajs/entrypoint": "^0.0.169",
|
|
32
|
+
"@aeriajs/types": "^0.0.137",
|
|
33
|
+
"mongodb": "^6.18.0"
|
|
34
34
|
},
|
|
35
35
|
"scripts": {
|
|
36
36
|
"test": "vitest run",
|