isvalid 2.9.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 CHANGED
@@ -14,11 +14,9 @@ const finalize = (formalizedSchema, nonFormalizedSchema) => {
14
14
  Object.defineProperty(formalizedSchema, '_nonFormalizedSchema', {
15
15
  value: nonFormalizedSchema,
16
16
  enumerable: false,
17
+ writable: false
17
18
  });
18
19
 
19
- // We seal the schema so no further editing can take place.
20
- Object.seal(formalizedSchema);
21
-
22
20
  return formalizedSchema;
23
21
 
24
22
  };
@@ -35,24 +33,16 @@ const formalizeObject = (formalizedSchema, nonFormalizedSchema, options) => {
35
33
  // We iterate through all keys.
36
34
  Object.keys(formalizedSchema.schema)
37
35
  .forEach((key) => {
38
-
39
- const formalizedKey = formalizeAny(formalizedSchema.schema[key], options);
40
-
41
- if (!options.throwDeepKeyOnImplicit) {
42
- if (formalizedSchema.required === undefined || formalizedSchema.required == 'implicit') {
43
- if (formalizedKey.required === true) formalizedSchema.required = true;
44
- }
45
- } else {
46
- if (formalizedSchema.required === undefined) {
47
- formalizedSchema.required = 'implicit';
48
- }
49
- }
50
-
51
- formalizedSubSchema[key] = formalizedKey;
52
-
36
+ formalizedSubSchema[key] = formalizeAny(formalizedSchema.schema[key], options);
53
37
  });
54
38
 
55
- 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
+ }
56
46
 
57
47
  return finalize(formalizedSchema, nonFormalizedSchema);
58
48
 
package/lib/index.js CHANGED
@@ -4,3 +4,4 @@ exports = module.exports = require('./validate.js');
4
4
  exports.formalize = require('./formalize.js');
5
5
  exports.validate = require('./middleware.js');
6
6
  exports.keyPaths = require('./key-paths');
7
+ exports.merge = require('./merge');
package/lib/key-paths.js CHANGED
@@ -3,37 +3,79 @@
3
3
  const formalize = require('./formalize'),
4
4
  utils = require('./utils');
5
5
 
6
- exports = module.exports = (schema, types, keyPath = []) => {
6
+ exports = module.exports = (schema, formalizeOptions) => {
7
7
 
8
- schema = formalize(schema);
8
+ schema = formalize(schema, formalizeOptions);
9
9
 
10
- if (!types) types = [];
11
- if (!Array.isArray(types)) types = [types];
10
+ return {
11
+ all: (types, keyPath = []) => {
12
12
 
13
- types = types.map((type) => utils.typeName(type));
13
+ if (!types) types = [];
14
+ if (!Array.isArray(types)) types = [types];
14
15
 
15
- let result = [];
16
+ types = types.map((type) => utils.typeName(type));
16
17
 
17
- const typeName = utils.typeName(schema.type);
18
+ let result = [];
18
19
 
19
- if (types.length == 0 || types.includes(typeName)) result.push(keyPath.join('.'));
20
+ const typeName = utils.typeName(schema.type);
20
21
 
21
- switch (typeName) {
22
- case 'object':
23
- result = result.concat(...Object.keys(schema.schema).map((key) => {
24
- return exports(schema.schema[key], types, keyPath.concat([key]));
25
- }));
26
- break;
27
- case 'array':
28
- result = result.concat(exports(schema.schema, types, keyPath));
29
- break;
30
- }
22
+ if (types.length == 0 || types.includes(typeName)) result.push(keyPath.join('.'));
31
23
 
32
- let uniqueResult = [];
33
- for (let keyPath of result) {
34
- if (!uniqueResult.includes(keyPath)) uniqueResult.push(keyPath);
35
- }
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;
33
+ }
36
34
 
37
- return uniqueResult;
35
+ let uniqueResult = [];
36
+ for (let keyPath of result) {
37
+ if (!uniqueResult.includes(keyPath)) uniqueResult.push(keyPath);
38
+ }
39
+
40
+ return uniqueResult;
41
+
42
+ },
43
+ get: (keyPath = '') => {
44
+
45
+ if (!Array.isArray(keyPath)) keyPath = keyPath.split('.').filter((key) => key);
46
+
47
+ if (keyPath.length === 0) return schema;
48
+
49
+ const typeName = utils.typeName(schema.type).toLowerCase();
50
+
51
+ switch (typeName) {
52
+ case 'object':
53
+ return exports(schema.schema[keyPath[0]]).get(keyPath.slice(1));
54
+ case 'array':
55
+ return exports(schema.schema).get(keyPath);
56
+ default:
57
+ throw new Error(`Cannot get key '${[keyPath[0]].concat(keyPath).join('.')}' from type '${typeName}'.`);
58
+ }
59
+
60
+ },
61
+ set: (keyPath, newSchema) => {
62
+
63
+ keyPath = keyPath || '';
64
+ newSchema = formalize(newSchema, formalizeOptions);
65
+
66
+ if (!Array.isArray(keyPath)) keyPath = keyPath.split('.').filter((key) => key);
67
+
68
+ if (keyPath.length === 0) return newSchema;
69
+
70
+ let parent = exports(schema).get(keyPath.slice(0, -1));
71
+
72
+ if (utils.typeName(parent.type).toLowerCase() !== 'object') throw new Error('Cannot set keys on non-object types.');
73
+
74
+ parent.schema[keyPath.slice(-1)[0]] = newSchema;
75
+
76
+ return schema;
77
+
78
+ }
79
+ };
38
80
 
39
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.9.0",
3
+ "version": "2.10.1",
4
4
  "description": "Async JSON validation library for node.js.",
5
5
  "main": "./index.js",
6
6
  "keywords": [
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/index.js CHANGED
@@ -5,11 +5,11 @@ const chai = require('chai'),
5
5
 
6
6
  chai.use(chaiAsPromised);
7
7
 
8
- require('./key-paths');
9
8
  require('./ranges.js');
10
9
  require('./equals.js');
11
10
  require('./unique.js');
12
11
  require('./formalize.js');
13
12
  require('./validate.js');
14
13
  require('./middleware/');
14
+ require('./key-paths');
15
15
  require('./merge');
package/test/key-paths.js CHANGED
@@ -7,16 +7,16 @@ describe('keyPaths', () => {
7
7
  it ('should come back with empty key paths', () => {
8
8
  expect(keyPaths({
9
9
  type: Boolean
10
- })).to.eql(['']);
10
+ }).all()).to.eql(['']);
11
11
  });
12
12
  it ('should come back with three key paths.', () => {
13
13
  expect(keyPaths({
14
14
  'first': Boolean,
15
15
  'second': String,
16
16
  'third': Number
17
- })).to.eql(['','first','second','third']);
17
+ }).all()).to.eql(['','first','second','third']);
18
18
  });
19
- it ('should come back with three eight paths.', () => {
19
+ it ('should come back with three key paths.', () => {
20
20
  expect(keyPaths({
21
21
  'first': [{
22
22
  'first': Boolean,
@@ -27,9 +27,9 @@ describe('keyPaths', () => {
27
27
  'second': String
28
28
  },
29
29
  'third': Number
30
- })).to.eql(['','first','first.first','first.second','second','second.first','second.second','third']);
30
+ }).all()).to.eql(['','first','first.first','first.second','second','second.first','second.second','third']);
31
31
  });
32
- it ('should come back with three eight paths.', () => {
32
+ it ('should come back with three key paths.', () => {
33
33
  expect(keyPaths({
34
34
  'first': [{
35
35
  'first': Boolean,
@@ -40,9 +40,9 @@ describe('keyPaths', () => {
40
40
  'second': String
41
41
  },
42
42
  'third': Number
43
- }, Object)).to.eql(['','first','second']);
43
+ }).all(Object)).to.eql(['','first','second']);
44
44
  });
45
- it ('should come back with three eight paths.', () => {
45
+ it ('should come back with two key paths.', () => {
46
46
  expect(keyPaths({
47
47
  'first': [{
48
48
  'first': Boolean,
@@ -53,9 +53,9 @@ describe('keyPaths', () => {
53
53
  'second': String
54
54
  },
55
55
  'third': Number
56
- }, Boolean)).to.eql(['first.first','second.first']);
56
+ }).all(Boolean)).to.eql(['first.first','second.first']);
57
57
  });
58
- it ('should come back with three eight paths.', () => {
58
+ it ('should come back with three key paths.', () => {
59
59
  expect(keyPaths({
60
60
  'first': [{
61
61
  'first': Boolean,
@@ -66,6 +66,30 @@ describe('keyPaths', () => {
66
66
  'second': String
67
67
  },
68
68
  'third': Number
69
- }, [Boolean, Array])).to.eql(['first','first.first','second.first']);
69
+ }).all([Boolean, Array])).to.eql(['first','first.first','second.first']);
70
+ });
71
+ it ('should come back with object at key (with intermediate array).', () => {
72
+ expect(keyPaths({
73
+ 'first': [{
74
+ 'first': Boolean,
75
+ 'second': String
76
+ }]
77
+ }).get('first.second')).to.have.property('type').equal(String);
78
+ });
79
+ it ('should come back with object at key.', () => {
80
+ expect(keyPaths({
81
+ 'second': {
82
+ 'first': Boolean,
83
+ 'second': String
84
+ }
85
+ }).get('second.second')).to.have.property('type').equal(String);
86
+ });
87
+ it ('should come back with root.', () => {
88
+ expect(keyPaths(String).get('')).to.have.property('type').equal(String);
89
+ });
90
+ it ('should come back with new validators set', () => {
91
+ expect(keyPaths({
92
+ 'test': String
93
+ }).set('my', Number)).to.have.property('schema').to.have.property('my').to.have.property('type').to.equal(Number);
70
94
  });
71
95
  });
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
  });