isvalid 2.10.0 → 2.10.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/lib/formalize.js +8 -16
- package/lib/key-paths.js +3 -3
- package/lib/merge.js +14 -10
- package/lib/validate.js +1 -1
- package/package.json +1 -1
- package/test/formalize.js +6 -6
- package/test/merge.js +5 -5
- package/test/validate.js +2 -14
package/lib/formalize.js
CHANGED
|
@@ -33,24 +33,16 @@ const formalizeObject = (formalizedSchema, nonFormalizedSchema, options) => {
|
|
|
33
33
|
// We iterate through all keys.
|
|
34
34
|
Object.keys(formalizedSchema.schema)
|
|
35
35
|
.forEach((key) => {
|
|
36
|
-
|
|
37
|
-
const formalizedKey = formalizeAny(formalizedSchema.schema[key], options);
|
|
38
|
-
|
|
39
|
-
if (!options.throwDeepKeyOnImplicit) {
|
|
40
|
-
if (formalizedSchema.required === undefined || formalizedSchema.required == 'implicit') {
|
|
41
|
-
if (formalizedKey.required === true) formalizedSchema.required = true;
|
|
42
|
-
}
|
|
43
|
-
} else {
|
|
44
|
-
if (formalizedSchema.required === undefined) {
|
|
45
|
-
formalizedSchema.required = 'implicit';
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
formalizedSubSchema[key] = formalizedKey;
|
|
50
|
-
|
|
36
|
+
formalizedSubSchema[key] = formalizeAny(formalizedSchema.schema[key], options);
|
|
51
37
|
});
|
|
52
38
|
|
|
53
|
-
formalizedSchema.schema = formalizedSubSchema
|
|
39
|
+
formalizedSchema.schema = formalizedSubSchema;
|
|
40
|
+
|
|
41
|
+
if (typeof formalizedSchema.required === 'undefined') {
|
|
42
|
+
if (Object.keys(formalizedSubSchema).some((key) => formalizedSubSchema[key].required === true || formalizedSubSchema[key].required === 'implicit')) {
|
|
43
|
+
formalizedSchema.required = 'implicit';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
54
46
|
|
|
55
47
|
return finalize(formalizedSchema, nonFormalizedSchema);
|
|
56
48
|
|
package/lib/key-paths.js
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
const formalize = require('./formalize'),
|
|
4
4
|
utils = require('./utils');
|
|
5
5
|
|
|
6
|
-
exports = module.exports = (schema) => {
|
|
6
|
+
exports = module.exports = (schema, formalizeOptions) => {
|
|
7
7
|
|
|
8
|
-
schema = formalize(schema);
|
|
8
|
+
schema = formalize(schema, formalizeOptions);
|
|
9
9
|
|
|
10
10
|
return {
|
|
11
11
|
all: (types, keyPath = []) => {
|
|
@@ -61,7 +61,7 @@ exports = module.exports = (schema) => {
|
|
|
61
61
|
set: (keyPath, newSchema) => {
|
|
62
62
|
|
|
63
63
|
keyPath = keyPath || '';
|
|
64
|
-
newSchema = formalize(newSchema);
|
|
64
|
+
newSchema = formalize(newSchema, formalizeOptions);
|
|
65
65
|
|
|
66
66
|
if (!Array.isArray(keyPath)) keyPath = keyPath.split('.').filter((key) => key);
|
|
67
67
|
|
package/lib/merge.js
CHANGED
|
@@ -5,10 +5,10 @@ const merge = require('merge');
|
|
|
5
5
|
const formalize = require('./formalize'),
|
|
6
6
|
utils = require('./utils');
|
|
7
7
|
|
|
8
|
-
const mergeSchema = (dest, src) => {
|
|
8
|
+
const mergeSchema = (dest, src, formalizeOptions) => {
|
|
9
9
|
|
|
10
|
-
dest = formalize(dest);
|
|
11
|
-
src = formalize(src);
|
|
10
|
+
dest = formalize(dest, formalizeOptions);
|
|
11
|
+
src = formalize(src, formalizeOptions);
|
|
12
12
|
|
|
13
13
|
const destType = utils.typeName(dest.type).toLowerCase();
|
|
14
14
|
const srcType = utils.typeName(src.type).toLowerCase();
|
|
@@ -34,22 +34,26 @@ const mergeSchema = (dest, src) => {
|
|
|
34
34
|
else schema[key] = mergeSchema(destSchema[key], srcSchema[key]);
|
|
35
35
|
});
|
|
36
36
|
|
|
37
|
-
return formalize(merge(formalize.strip(dest), formalize.strip(src), { schema }));
|
|
37
|
+
return formalize(merge(formalize.strip(dest), formalize.strip(src), { schema }), formalizeOptions);
|
|
38
38
|
|
|
39
39
|
}
|
|
40
40
|
case 'array': {
|
|
41
41
|
return merge(dest, src, {
|
|
42
|
-
schema: formalize(mergeSchema(formalize.strip(dest.schema), formalize.strip(src.schema)))
|
|
42
|
+
schema: formalize(mergeSchema(formalize.strip(dest.schema), formalize.strip(src.schema)), formalizeOptions)
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
45
|
default:
|
|
46
|
-
return formalize(merge(formalize.strip(dest), formalize.strip(src)));
|
|
46
|
+
return formalize(merge(formalize.strip(dest), formalize.strip(src)), formalizeOptions);
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
};
|
|
50
50
|
|
|
51
|
-
exports = module.exports = (dest,
|
|
52
|
-
return
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
exports = module.exports = (dest, formalizeOptions) => {
|
|
52
|
+
return {
|
|
53
|
+
with: (...sources) => {
|
|
54
|
+
return sources.reduce((result, current) => {
|
|
55
|
+
return mergeSchema(result, current, formalizeOptions);
|
|
56
|
+
}, dest);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
55
59
|
};
|
package/lib/validate.js
CHANGED
|
@@ -323,7 +323,7 @@ const validateAny = async (data, schema, options, keyPath, validatedData) => {
|
|
|
323
323
|
data = await Promise.resolve(data);
|
|
324
324
|
return await validatePost(data, schema, options, keyPath, validatedData);
|
|
325
325
|
}
|
|
326
|
-
if (
|
|
326
|
+
if (schema.required === 'implicit') {
|
|
327
327
|
data = {};
|
|
328
328
|
} else if (checkBoolValue('required', schema, options.defaults)) {
|
|
329
329
|
throw new ValidationError(
|
package/package.json
CHANGED
package/test/formalize.js
CHANGED
|
@@ -83,24 +83,24 @@ describe('schema', function() {
|
|
|
83
83
|
it('should come back with a Date shortcut expanded.', () => {
|
|
84
84
|
expect(formalize(Date)).to.have.property('type').equal(Date);
|
|
85
85
|
});
|
|
86
|
-
it('should come back with required set to
|
|
86
|
+
it('should come back with required set to `implicit` if object has not specified required and a nested sub-schema is required.', () => {
|
|
87
87
|
expect(formalize({
|
|
88
88
|
'a': { type: String, required: true }
|
|
89
|
-
})).to.have.property('required').to.be.equal(
|
|
89
|
+
})).to.have.property('required').to.be.equal('implicit');
|
|
90
90
|
});
|
|
91
|
-
it('should come back with required set to
|
|
91
|
+
it('should come back with required set to `implicit` if any deep sub-schema is required.', () => {
|
|
92
92
|
expect(formalize({
|
|
93
93
|
'a': {
|
|
94
94
|
'b': {
|
|
95
95
|
'c': { type: String, required: true }
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
|
-
})).to.have.property('required').to.be.equal(
|
|
98
|
+
})).to.have.property('required').to.be.equal('implicit');
|
|
99
99
|
});
|
|
100
|
-
it('should come back with required set to
|
|
100
|
+
it('should come back with required set to `implicit` if root object has required in sub-schema.', () => {
|
|
101
101
|
expect(formalize({
|
|
102
102
|
'a': { type: String, required: true }
|
|
103
|
-
})).to.have.property('required').to.be.equal(
|
|
103
|
+
})).to.have.property('required').to.be.equal('implicit');
|
|
104
104
|
});
|
|
105
105
|
it('should come back with required set to false if root object required is false and deep sub-schema is required.', () => {
|
|
106
106
|
expect(formalize({
|
package/test/merge.js
CHANGED
|
@@ -5,14 +5,14 @@ const expect = require('chai').expect,
|
|
|
5
5
|
|
|
6
6
|
describe('merge', () => {
|
|
7
7
|
it ('should come back with destination type.', () => {
|
|
8
|
-
expect(merge({ type: String }
|
|
8
|
+
expect(merge({ type: String }).with({ type: Date })).to.have.property('type').equal(Date);
|
|
9
9
|
});
|
|
10
10
|
it ('should come back with merged validators', () => {
|
|
11
|
-
expect(merge({ type: String, required: true }
|
|
12
|
-
expect(merge({ type: String }
|
|
11
|
+
expect(merge({ type: String, required: true }).with({ type: String })).to.have.property('required').equal(true);
|
|
12
|
+
expect(merge({ type: String }).with({ type: String, required: true })).to.have.property('required').equal(true);
|
|
13
13
|
});
|
|
14
14
|
it ('should come back with array', () => {
|
|
15
|
-
const result = merge([{ type: String }]
|
|
15
|
+
const result = merge([{ type: String }]).with([{ type: Date }]);
|
|
16
16
|
expect(result).to.have.property('type').equal(Array);
|
|
17
17
|
expect(result.schema).to.have.property('type').equal(Date);
|
|
18
18
|
});
|
|
@@ -20,7 +20,7 @@ describe('merge', () => {
|
|
|
20
20
|
const result = merge({
|
|
21
21
|
'this': { type: String },
|
|
22
22
|
'is': { type: String}
|
|
23
|
-
}
|
|
23
|
+
}).with({
|
|
24
24
|
'is': { type: String, required: true },
|
|
25
25
|
'a': { type: Number },
|
|
26
26
|
'test': { type: Date}
|
package/test/validate.js
CHANGED
|
@@ -448,26 +448,14 @@ describe('validate', function() {
|
|
|
448
448
|
})).to.eventually.not.have.property('why');
|
|
449
449
|
});
|
|
450
450
|
describe('required', function() {
|
|
451
|
-
it ('should come back with object key path when implicit
|
|
452
|
-
return expect(isvalid(
|
|
453
|
-
'myObject': {
|
|
454
|
-
'myKey': {
|
|
455
|
-
type: String,
|
|
456
|
-
required: true
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
})).to.eventually.be.rejectedWith(ValidationError).and.have.property('keyPath').to.have.members(['myObject']);
|
|
460
|
-
});
|
|
461
|
-
it ('should come back with object key path when implicit and `throwDeepKeyOnImplicit` is `true`.', () => {
|
|
462
|
-
return expect(isvalid({}, {
|
|
451
|
+
it ('should come back with object key path when implicit.', () => {
|
|
452
|
+
return expect(isvalid(undefined, {
|
|
463
453
|
'myObject': {
|
|
464
454
|
'myKey': {
|
|
465
455
|
type: String,
|
|
466
456
|
required: true
|
|
467
457
|
}
|
|
468
458
|
}
|
|
469
|
-
}, {
|
|
470
|
-
throwDeepKeyOnImplicit: true
|
|
471
459
|
})).to.eventually.be.rejectedWith(ValidationError).and.have.property('keyPath').to.have.members(['myObject', 'myKey']);
|
|
472
460
|
});
|
|
473
461
|
});
|