isvalid 4.0.20 → 4.0.22
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/lib/formalize.js +28 -10
- package/lib/validate.js +2 -2
- package/package.json +45 -45
- package/test/equals.js +4 -4
- package/test/formalize.js +12 -1
- package/test/middleware/tools/server.js +1 -1
- package/test/validate.js +1 -1
package/lib/formalize.js
CHANGED
|
@@ -136,7 +136,7 @@ const formalizeAny = (schema, options = {}) => {
|
|
|
136
136
|
'len': [ 'string', 'number' ],
|
|
137
137
|
'match': [ 'regexp' ],
|
|
138
138
|
'trim': [ 'boolean' ],
|
|
139
|
-
'enum': [ 'array' ]
|
|
139
|
+
'enum': [ 'array', 'object' ]
|
|
140
140
|
});
|
|
141
141
|
if (isSameType('number', typeName(type))) merge(validators, {
|
|
142
142
|
'range': [ 'string', 'number', testFormalizedRange ],
|
|
@@ -293,20 +293,38 @@ const formalizeAny = (schema, options = {}) => {
|
|
|
293
293
|
|
|
294
294
|
// Check string enums
|
|
295
295
|
if (typeof formalizedSchema.enum !== 'undefined') {
|
|
296
|
-
|
|
296
|
+
|
|
297
|
+
let wasArray = false;
|
|
298
|
+
|
|
299
|
+
if (Array.isArray(formalizedSchema.enum)) {
|
|
300
|
+
wasArray = true;
|
|
301
|
+
for (let value of formalizedSchema.enum) {
|
|
302
|
+
if (typeof value !== 'string') {
|
|
303
|
+
throw new SchemaError(
|
|
304
|
+
schema,
|
|
305
|
+
wasArray ? 'Validator `enum` must be an array of strings.' : 'Validator `enum` must be an object with string keys.'
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
formalizedSchema.enum = Object.fromEntries(formalizedSchema.enum.map((value) => [value, value]));
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (typeof formalizedSchema.enum !== 'object' || formalizedSchema.enum === null) {
|
|
297
313
|
throw new SchemaError(
|
|
298
314
|
schema,
|
|
299
|
-
'Validator `enum` must
|
|
315
|
+
'Validator `enum` must be an array or object.'
|
|
300
316
|
);
|
|
301
317
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
318
|
+
|
|
319
|
+
const keys = Object.keys(formalizedSchema.enum);
|
|
320
|
+
|
|
321
|
+
if (keys.length < 1) {
|
|
322
|
+
throw new SchemaError(
|
|
323
|
+
schema,
|
|
324
|
+
'Validator `enum` must have at least one item.'
|
|
325
|
+
);
|
|
309
326
|
}
|
|
327
|
+
|
|
310
328
|
}
|
|
311
329
|
|
|
312
330
|
// Add priority if not added.
|
package/lib/validate.js
CHANGED
|
@@ -165,13 +165,13 @@ const validateString = async (data, schema, options, keyPath) => {
|
|
|
165
165
|
}
|
|
166
166
|
|
|
167
167
|
// Validate enums
|
|
168
|
-
if ((schema.enum || options.defaults.enum) && (schema.enum || options.defaults.enum).indexOf(data) == -1) {
|
|
168
|
+
if ((schema.enum || options.defaults.enum) && Object.keys(schema.enum || options.defaults.enum).indexOf(data) == -1) {
|
|
169
169
|
throw new ValidationError(
|
|
170
170
|
keyPath,
|
|
171
171
|
schema._nonFormalizedSchema,
|
|
172
172
|
'enum',
|
|
173
173
|
(schema.errors || {}).enum || customErrorMessage(((options.errorMessages || {}).string || {}).enum || ((values) => {
|
|
174
|
-
return `Possible values are ${values.map(function(val) {
|
|
174
|
+
return `Possible values are ${Object.keys(values).map(function(val) {
|
|
175
175
|
return '"' + val + '"';
|
|
176
176
|
}).reduce(function(prev, cur, idx, arr) {
|
|
177
177
|
return prev + (idx == arr.length - 1 ? ' and ' : ', ') + cur;
|
package/package.json
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
2
|
+
"name": "isvalid",
|
|
3
|
+
"version": "4.0.22",
|
|
4
|
+
"description": "Async JSON validation library for node.js.",
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"schema",
|
|
9
|
+
"validation",
|
|
10
|
+
"JSON",
|
|
11
|
+
"rest",
|
|
12
|
+
"api",
|
|
13
|
+
"blob",
|
|
14
|
+
"validate",
|
|
15
|
+
"express",
|
|
16
|
+
"connect"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"merge": "^2.1.1"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@trenskow/caseit": "^1.3.11",
|
|
23
|
+
"body-parser": "^1.20.2",
|
|
24
|
+
"chai": "^5.0.0",
|
|
25
|
+
"chai-as-promised": "^7.1.1",
|
|
26
|
+
"eslint": "^8.56.0",
|
|
27
|
+
"express": "^4.18.2",
|
|
28
|
+
"mocha": "^10.2.0",
|
|
29
|
+
"supertest": "^6.3.4"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"test": "./node_modules/mocha/bin/mocha.js ./test/index.js"
|
|
33
|
+
},
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/trenskow/isvalid.git"
|
|
37
|
+
},
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"author": {
|
|
40
|
+
"name": "Kristian Trenskow",
|
|
41
|
+
"email": "trenskow@me.com",
|
|
42
|
+
"url": "https://github.com/trenskow"
|
|
43
|
+
},
|
|
44
|
+
"overrides": {
|
|
45
|
+
"chai": "^5.0.0"
|
|
46
|
+
}
|
|
47
47
|
}
|
package/test/equals.js
CHANGED
|
@@ -44,14 +44,14 @@ describe('equals', function() {
|
|
|
44
44
|
it('should return false if dates are not equal.', () => {
|
|
45
45
|
return expect(equals(d1, d2)).to.eventually.be.false;
|
|
46
46
|
});
|
|
47
|
-
it
|
|
47
|
+
it('should return true if objects are equal.', () => {
|
|
48
48
|
return expect(equals({
|
|
49
49
|
awesome: true
|
|
50
50
|
}, {
|
|
51
51
|
awesome: true
|
|
52
52
|
})).to.eventually.be.true;
|
|
53
53
|
});
|
|
54
|
-
it
|
|
54
|
+
it('should return false if object are not equal.', () => {
|
|
55
55
|
return expect(equals({
|
|
56
56
|
awesome: true
|
|
57
57
|
}, {
|
|
@@ -64,14 +64,14 @@ describe('equals', function() {
|
|
|
64
64
|
it('should return false if arrays are not equal.', () => {
|
|
65
65
|
return expect(equals(['This','is','an','array'], ['This','is','another','array'])).to.eventually.be.false;
|
|
66
66
|
});
|
|
67
|
-
it
|
|
67
|
+
it('should return true if objects with arrays are equal.', () => {
|
|
68
68
|
return expect(equals({
|
|
69
69
|
obj: ['This','is','an','array']
|
|
70
70
|
}, {
|
|
71
71
|
obj: ['This','is','an','array']
|
|
72
72
|
})).to.eventually.be.true;
|
|
73
73
|
});
|
|
74
|
-
it
|
|
74
|
+
it('should return false if objects with arrays are not equal.', () => {
|
|
75
75
|
return expect(equals({
|
|
76
76
|
obj: ['This','is','an','array']
|
|
77
77
|
}, {
|
package/test/formalize.js
CHANGED
|
@@ -42,13 +42,24 @@ describe('schema', function() {
|
|
|
42
42
|
it('should come back with enum intact', () => {
|
|
43
43
|
expect(formalize({ type: String, enum: ['this', 'test']}))
|
|
44
44
|
.to.have.property('enum')
|
|
45
|
-
.to.
|
|
45
|
+
.to.eql({
|
|
46
|
+
'this': 'this',
|
|
47
|
+
'test': 'test'
|
|
48
|
+
});
|
|
46
49
|
});
|
|
47
50
|
it('should come back with enum intact (custom error message)', () => {
|
|
48
51
|
expect(formalize({ type: String, enum: [['this', 'test'], 'Must be this or test.'] }))
|
|
49
52
|
.to.have.property('errors')
|
|
50
53
|
.to.have.property('enum', 'Must be this or test.');
|
|
51
54
|
});
|
|
55
|
+
it('should come back with enum allowed if it is an object.', () => {
|
|
56
|
+
expect(formalize({ type: String, enum: { this: 'this is', test: 'a test' } }))
|
|
57
|
+
.to.have.property('enum')
|
|
58
|
+
.to.eql({
|
|
59
|
+
'this': 'this is',
|
|
60
|
+
'test': 'a test'
|
|
61
|
+
});
|
|
62
|
+
});
|
|
52
63
|
it('should throw an error if schema type cannot be determined.', () => {
|
|
53
64
|
expect(f({ type: '' })).to.throw(SchemaError);
|
|
54
65
|
});
|
package/test/validate.js
CHANGED
|
@@ -309,7 +309,7 @@ const commonTests = {
|
|
|
309
309
|
});
|
|
310
310
|
});
|
|
311
311
|
},
|
|
312
|
-
all: function(type, validData, invalidData) {
|
|
312
|
+
all: function(type, validData, invalidData) { let self = this;
|
|
313
313
|
['type', 'required', 'null', 'default', 'equal', 'post'].forEach(function(test) {
|
|
314
314
|
self[test](type, validData, invalidData);
|
|
315
315
|
});
|