isvalid 2.10.0 → 2.10.4

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 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,12 +3,22 @@
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
- all: (types, keyPath = []) => {
11
+ all: (types, options, keyPath = [], level = 0) => {
12
+
13
+ if (typeof types === 'object' && types !== null && !Array.isArray(types)) {
14
+ options = types;
15
+ types = [];
16
+ }
17
+
18
+ options = options || {};
19
+ options.maxDepth = typeof options.maxDepth !== 'undefined' ? options.maxDepth : Infinity;
20
+
21
+ if (typeof options.maxDepth !== 'number') throw new Error('Maximum depth must be a number.');
12
22
 
13
23
  if (!types) types = [];
14
24
  if (!Array.isArray(types)) types = [types];
@@ -21,15 +31,19 @@ exports = module.exports = (schema) => {
21
31
 
22
32
  if (types.length == 0 || types.includes(typeName)) result.push(keyPath.join('.'));
23
33
 
24
- switch (typeName) {
25
- case 'object':
26
- result = result.concat(...Object.keys(schema.schema).map((key) => {
27
- return exports(schema.schema[key]).all(types, keyPath.concat([key]));
28
- }));
29
- break;
30
- case 'array':
31
- result = result.concat(exports(schema.schema).all(types, keyPath));
32
- break;
34
+ if (level < options.maxDepth) {
35
+
36
+ switch (typeName) {
37
+ case 'object':
38
+ result = result.concat(...Object.keys(schema.schema).map((key) => {
39
+ return exports(schema.schema[key]).all(types, options, keyPath.concat([key]), level + 1);
40
+ }));
41
+ break;
42
+ case 'array':
43
+ result = result.concat(exports(schema.schema).all(types, options, keyPath, level + 1));
44
+ break;
45
+ }
46
+
33
47
  }
34
48
 
35
49
  let uniqueResult = [];
@@ -61,7 +75,7 @@ exports = module.exports = (schema) => {
61
75
  set: (keyPath, newSchema) => {
62
76
 
63
77
  keyPath = keyPath || '';
64
- newSchema = formalize(newSchema);
78
+ newSchema = formalize(newSchema, formalizeOptions);
65
79
 
66
80
  if (!Array.isArray(keyPath)) keyPath = keyPath.split('.').filter((key) => key);
67
81
 
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, ...sources) => {
52
- return sources.reduce((result, current) => {
53
- return mergeSchema(result, current);
54
- }, dest);
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 (options.throwDeepKeyOnImplicit && schema.required === 'implicit') {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "isvalid",
3
- "version": "2.10.0",
3
+ "version": "2.10.4",
4
4
  "description": "Async JSON validation library for node.js.",
5
5
  "main": "./index.js",
6
6
  "keywords": [
@@ -21,10 +21,10 @@
21
21
  "body-parser": "^1.19.0",
22
22
  "chai": "^4.2.0",
23
23
  "chai-as-promised": "^7.1.1",
24
- "eslint": "^7.18.0",
24
+ "eslint": "^8.2.0",
25
25
  "express": "^4.17.1",
26
- "mocha": "^8.2.1",
27
- "supertest": "^3.4.2"
26
+ "mocha": "^9.1.3",
27
+ "supertest": "^6.1.6"
28
28
  },
29
29
  "scripts": {
30
30
  "test": "./node_modules/mocha/bin/mocha ./test/index.js"
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 true if object has not specified required and a nested sub-schema is required.', () => {
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(true);
89
+ })).to.have.property('required').to.be.equal('implicit');
90
90
  });
91
- it('should come back with required set to true if any deep sub-schema is required.', () => {
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(true);
98
+ })).to.have.property('required').to.be.equal('implicit');
99
99
  });
100
- it('should come back with required set to true if root object has required in sub-schema.', () => {
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(true);
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 }, { type: Date })).to.have.property('type').equal(Date);
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 }, { type: String })).to.have.property('required').equal(true);
12
- expect(merge({ type: String }, { type: String, required: true })).to.have.property('required').equal(true);
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 }], [{ type: Date }]);
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 and no `throwDeepKeyOnImplicit`.', () => {
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
  });