isvalid 4.0.14 → 4.0.15

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
@@ -9,7 +9,7 @@
9
9
  import merge from 'merge';
10
10
  import SchemaError from './errors/schema.js';
11
11
  import { instanceTypeName, typeName, isSameType } from './utils.js';
12
- import { formalize as _formalize } from './ranges.js';
12
+ import { formalize as _formalizeRange, testFormalizedRange } from './ranges.js';
13
13
  import { all as allPlugins } from './plugins.js';
14
14
 
15
15
  const finalize = (formalizedSchema, nonFormalizedSchema) => {
@@ -128,7 +128,7 @@ const formalizeAny = (schema, options = {}) => {
128
128
  });
129
129
  if (isSameType('array', typeName(type))) merge(validators, {
130
130
  'schema': 'any',
131
- 'len': [ 'string', 'number' ],
131
+ 'len': [ 'string', 'number', testFormalizedRange ],
132
132
  'unique': [ 'boolean' ],
133
133
  'autoWrap': [ 'boolean' ]
134
134
  });
@@ -139,7 +139,7 @@ const formalizeAny = (schema, options = {}) => {
139
139
  'enum': [ 'array' ]
140
140
  });
141
141
  if (isSameType('number', typeName(type))) merge(validators, {
142
- 'range': [ 'string', 'number' ],
142
+ 'range': [ 'string', 'number', testFormalizedRange ],
143
143
  'float': [ 'string' ]
144
144
  });
145
145
  }
@@ -203,10 +203,10 @@ const formalizeAny = (schema, options = {}) => {
203
203
  }
204
204
 
205
205
  // Ensure validator is of correct type.
206
- if (typeof validator !== 'undefined' && validator !== 'any' && validator.indexOf(instanceTypeName(test)) == -1) {
206
+ if (typeof validator !== 'undefined' && validator !== 'any' && Array.isArray(validator) && !validator.some((validator) => typeof validator === 'function' ? validator(test) : validator === instanceTypeName(test))) {
207
207
  throw new SchemaError(
208
208
  schema,
209
- `Validator '${key}' must be of type(s) ${validators[key].join(', ')}.`
209
+ `Validator '${key}' must be of type(s) ${validators[key].filter((validator) => typeof validator === 'string').join(', ')}.`
210
210
  );
211
211
  }
212
212
 
@@ -277,7 +277,7 @@ const formalizeAny = (schema, options = {}) => {
277
277
 
278
278
  // Check len
279
279
  if (typeof formalizedSchema.len !== 'undefined') {
280
- formalizedSchema.len = _formalize(formalizedSchema.len, {
280
+ formalizedSchema.len = _formalizeRange(formalizedSchema.len, {
281
281
  allowNegative: false,
282
282
  allowNonIntegers: false
283
283
  });
@@ -285,7 +285,7 @@ const formalizeAny = (schema, options = {}) => {
285
285
 
286
286
  // Check range
287
287
  if (typeof formalizedSchema.range !== 'undefined') {
288
- formalizedSchema.range = _formalize(formalizedSchema.range, {
288
+ formalizedSchema.range = _formalizeRange(formalizedSchema.range, {
289
289
  allowNegative: true,
290
290
  allowNonIntegers: true
291
291
  });
package/lib/ranges.js CHANGED
@@ -8,8 +8,19 @@ export function testIndex(ranges, value) {
8
8
  });
9
9
  }
10
10
 
11
+ export function testFormalizedRange(range) {
12
+ if (!Array.isArray(range)) return false;
13
+ if (range.some((range) => typeof range !== 'object')) return false;
14
+ return range.every((range) => {
15
+ if (Object.keys(range).length != 2) return false;
16
+ return ['lower', 'upper'].every((key) => typeof range[key] !== 'undefined');
17
+ });
18
+ }
19
+
11
20
  export function formalize(ranges, options) {
12
21
 
22
+ if (testFormalizedRange(ranges)) return ranges;
23
+
13
24
  // Convert to string if ranges is a Number.
14
25
  if (ranges !== undefined && typeof ranges === 'number') {
15
26
  ranges = ranges.toString();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "isvalid",
3
- "version": "4.0.14",
3
+ "version": "4.0.15",
4
4
  "description": "Async JSON validation library for node.js.",
5
5
  "main": "./index.js",
6
6
  "type": "module",
package/test/ranges.js CHANGED
@@ -47,4 +47,10 @@ describe('ranges', function() {
47
47
  it ('should allow for decimal values.', function() {
48
48
  expect(r('(-2.2)-2.2', 0.1)).to.equal(true);
49
49
  });
50
+ it ('should allow for already formalized ranges.', function() {
51
+ expect(r([{
52
+ lower: 0,
53
+ upper: 2
54
+ }], 1)).to.equal(true);
55
+ });
50
56
  });