isvalid 2.10.2 → 2.10.5

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
@@ -77,7 +77,7 @@ const formalizeAny = (schema, options = {}) => {
77
77
  return formalizeObject({ type: Object, schema: schema }, schema, options);
78
78
  }
79
79
  if ('array' == utils.instanceTypeName(schema)) {
80
- if (schema.length === 0) throw new SchemaError(schema, 'Array must have exactly one schema.');
80
+ if (schema.length === 0) return formalizeAny({ type: Array }, schema. options);
81
81
  return formalizeArray({ type: Array, schema: schema[0] }, schema, options);
82
82
  }
83
83
  if ((typeof schema === 'string' && schema.length) || (typeof schema === 'function' && utils.typeName(schema) !== undefined)) {
package/lib/key-paths.js CHANGED
@@ -8,7 +8,17 @@ exports = module.exports = (schema, formalizeOptions) => {
8
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, formalizeOptions) => {
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 = [];
package/lib/validate.js CHANGED
@@ -30,6 +30,9 @@ const validateObject = async (data, schema, options, keyPath, validatedData) =>
30
30
  );
31
31
  }
32
32
 
33
+ // If there is no schema we just return the object.
34
+ if (typeof schema.schema === 'undefined') return data;
35
+
33
36
  // Find unknown keys
34
37
  for (let key in data) {
35
38
  if (schema.schema[key] === undefined) {
@@ -87,9 +90,11 @@ const validateArray = async (data, schema, options, keyPath, validatedData) => {
87
90
  }
88
91
  }
89
92
 
90
- data = await Promise.all(data.map((data, idx) => {
91
- return validateAny(data, schema.schema, options, keyPath.concat([idx]), validatedData);
92
- }));
93
+ if (typeof schema.schema !== 'undefined') {
94
+ data = await Promise.all(data.map((data, idx) => {
95
+ return validateAny(data, schema.schema, options, keyPath.concat([idx]), validatedData);
96
+ }));
97
+ }
93
98
 
94
99
  if ((schema.len || options.defaults.len) && !ranges.testIndex((schema.len || options.defaults.len), data.length)) {
95
100
  throw new ValidationError(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "isvalid",
3
- "version": "2.10.2",
3
+ "version": "2.10.5",
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
@@ -12,9 +12,6 @@ const f = (...args) => {
12
12
 
13
13
  describe('schema', function() {
14
14
  describe('formalizer', function() {
15
- it('should throw an error if array shortcut contains no object.', () => {
16
- expect(f([])).to.throw(SchemaError);
17
- });
18
15
  it('should throw an error if schema is garbage value.', () => {
19
16
  expect(f(123)).to.throw(SchemaError);
20
17
  });
package/test/validate.js CHANGED
@@ -360,6 +360,9 @@ describe('validate', function() {
360
360
  });
361
361
  describe('object validator', function() {
362
362
  commonTests.all(Object, {}, 123);
363
+ it('should come back with same input if sub-schema is not provided.', () => {
364
+ return expect(isvalid({}, {})).to.eventually.eql({});
365
+ });
363
366
  it('should come out with same input as output if keys can validate.', () => {
364
367
  let s = isvalid({
365
368
  awesome: true,
@@ -480,7 +483,10 @@ describe('validate', function() {
480
483
  });
481
484
  describe('array validator', function() {
482
485
  commonTests.all(Array, [], 123);
483
- it('should come out with same input as output if array can validate.', () => {
486
+ it('should come back with same input if no sub-schema is provided.', () => {
487
+ return expect(isvalid([], [])).to.eventually.eql([]);
488
+ });
489
+ it('should come back with same input as output if array can validate.', () => {
484
490
  let s = isvalid([{
485
491
  awesome: true,
486
492
  why: 'it just is!'