isvalid 4.0.20 → 4.0.21

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
@@ -293,20 +293,38 @@ const formalizeAny = (schema, options = {}) => {
293
293
 
294
294
  // Check string enums
295
295
  if (typeof formalizedSchema.enum !== 'undefined') {
296
- if (formalizedSchema.enum.length < 1) {
296
+
297
+ let wasArray = false;
298
+
299
+ if (Array.isArray(formalizedSchema.enum)) {
300
+ wasArray = true;
301
+ for (let value of formalizedSchema.enum) {
302
+ if (typeof value !== 'string') {
303
+ throw new SchemaError(
304
+ schema,
305
+ wasArray ? 'Validator `enum` must be an array of strings.' : 'Validator `enum` must be an object with string keys.'
306
+ );
307
+ }
308
+ }
309
+ formalizedSchema.enum = Object.fromEntries(formalizedSchema.enum.map((value) => [value, value]));
310
+ }
311
+
312
+ if (typeof formalizedSchema.enum !== 'object' || formalizedSchema.enum === null) {
297
313
  throw new SchemaError(
298
314
  schema,
299
- 'Validator `enum` must have at least one item.'
315
+ 'Validator `enum` must be an array or object.'
300
316
  );
301
317
  }
302
- for (var idx in formalizedSchema.enum) {
303
- if (typeof formalizedSchema.enum[idx] !== 'string') {
304
- throw new SchemaError(
305
- schema,
306
- 'Validator `enum` must be an array of strings.'
307
- );
308
- }
318
+
319
+ const keys = Object.keys(formalizedSchema.enum);
320
+
321
+ if (keys.length < 1) {
322
+ throw new SchemaError(
323
+ schema,
324
+ 'Validator `enum` must have at least one item.'
325
+ );
309
326
  }
327
+
310
328
  }
311
329
 
312
330
  // Add priority if not added.
package/lib/validate.js CHANGED
@@ -165,13 +165,13 @@ const validateString = async (data, schema, options, keyPath) => {
165
165
  }
166
166
 
167
167
  // Validate enums
168
- if ((schema.enum || options.defaults.enum) && (schema.enum || options.defaults.enum).indexOf(data) == -1) {
168
+ if ((schema.enum || options.defaults.enum) && Object.keys(schema.enum || options.defaults.enum).indexOf(data) == -1) {
169
169
  throw new ValidationError(
170
170
  keyPath,
171
171
  schema._nonFormalizedSchema,
172
172
  'enum',
173
173
  (schema.errors || {}).enum || customErrorMessage(((options.errorMessages || {}).string || {}).enum || ((values) => {
174
- return `Possible values are ${values.map(function(val) {
174
+ return `Possible values are ${Object.keys(values).map(function(val) {
175
175
  return '"' + val + '"';
176
176
  }).reduce(function(prev, cur, idx, arr) {
177
177
  return prev + (idx == arr.length - 1 ? ' and ' : ', ') + cur;
package/package.json CHANGED
@@ -1,47 +1,47 @@
1
1
  {
2
- "name": "isvalid",
3
- "version": "4.0.20",
4
- "description": "Async JSON validation library for node.js.",
5
- "main": "./index.js",
6
- "type": "module",
7
- "keywords": [
8
- "schema",
9
- "validation",
10
- "JSON",
11
- "rest",
12
- "api",
13
- "blob",
14
- "validate",
15
- "express",
16
- "connect"
17
- ],
18
- "dependencies": {
19
- "merge": "^2.1.1"
20
- },
21
- "devDependencies": {
22
- "@trenskow/caseit": "^1.3.11",
23
- "body-parser": "^1.20.2",
24
- "chai": "^5.0.0",
25
- "chai-as-promised": "^7.1.1",
26
- "eslint": "^8.56.0",
27
- "express": "^4.18.2",
28
- "mocha": "^10.2.0",
29
- "supertest": "^6.3.4"
30
- },
31
- "scripts": {
32
- "test": "./node_modules/mocha/bin/mocha.js ./test/index.js"
33
- },
34
- "repository": {
35
- "type": "git",
36
- "url": "https://github.com/trenskow/isvalid.git"
37
- },
38
- "license": "MIT",
39
- "author": {
40
- "name": "Kristian Trenskow",
41
- "email": "trenskow@me.com",
42
- "url": "https://github.com/trenskow"
43
- },
44
- "overrides": {
45
- "chai": "^5.0.0"
46
- }
2
+ "name": "isvalid",
3
+ "version": "4.0.21",
4
+ "description": "Async JSON validation library for node.js.",
5
+ "main": "./index.js",
6
+ "type": "module",
7
+ "keywords": [
8
+ "schema",
9
+ "validation",
10
+ "JSON",
11
+ "rest",
12
+ "api",
13
+ "blob",
14
+ "validate",
15
+ "express",
16
+ "connect"
17
+ ],
18
+ "dependencies": {
19
+ "merge": "^2.1.1"
20
+ },
21
+ "devDependencies": {
22
+ "@trenskow/caseit": "^1.3.11",
23
+ "body-parser": "^1.20.2",
24
+ "chai": "^5.0.0",
25
+ "chai-as-promised": "^7.1.1",
26
+ "eslint": "^8.56.0",
27
+ "express": "^4.18.2",
28
+ "mocha": "^10.2.0",
29
+ "supertest": "^6.3.4"
30
+ },
31
+ "scripts": {
32
+ "test": "./node_modules/mocha/bin/mocha.js ./test/index.js"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "https://github.com/trenskow/isvalid.git"
37
+ },
38
+ "license": "MIT",
39
+ "author": {
40
+ "name": "Kristian Trenskow",
41
+ "email": "trenskow@me.com",
42
+ "url": "https://github.com/trenskow"
43
+ },
44
+ "overrides": {
45
+ "chai": "^5.0.0"
46
+ }
47
47
  }
package/test/equals.js CHANGED
@@ -44,14 +44,14 @@ describe('equals', function() {
44
44
  it('should return false if dates are not equal.', () => {
45
45
  return expect(equals(d1, d2)).to.eventually.be.false;
46
46
  });
47
- it ('should return true if objects are equal.', () => {
47
+ it('should return true if objects are equal.', () => {
48
48
  return expect(equals({
49
49
  awesome: true
50
50
  }, {
51
51
  awesome: true
52
52
  })).to.eventually.be.true;
53
53
  });
54
- it ('should return false if object are not equal.', () => {
54
+ it('should return false if object are not equal.', () => {
55
55
  return expect(equals({
56
56
  awesome: true
57
57
  }, {
@@ -64,14 +64,14 @@ describe('equals', function() {
64
64
  it('should return false if arrays are not equal.', () => {
65
65
  return expect(equals(['This','is','an','array'], ['This','is','another','array'])).to.eventually.be.false;
66
66
  });
67
- it ('should return true if objects with arrays are equal.', () => {
67
+ it('should return true if objects with arrays are equal.', () => {
68
68
  return expect(equals({
69
69
  obj: ['This','is','an','array']
70
70
  }, {
71
71
  obj: ['This','is','an','array']
72
72
  })).to.eventually.be.true;
73
73
  });
74
- it ('should return false if objects with arrays are not equal.', () => {
74
+ it('should return false if objects with arrays are not equal.', () => {
75
75
  return expect(equals({
76
76
  obj: ['This','is','an','array']
77
77
  }, {
package/test/formalize.js CHANGED
@@ -42,7 +42,10 @@ describe('schema', function() {
42
42
  it('should come back with enum intact', () => {
43
43
  expect(formalize({ type: String, enum: ['this', 'test']}))
44
44
  .to.have.property('enum')
45
- .to.have.property(1, 'test');
45
+ .to.eql({
46
+ 'this': 'this',
47
+ 'test': 'test'
48
+ });
46
49
  });
47
50
  it('should come back with enum intact (custom error message)', () => {
48
51
  expect(formalize({ type: String, enum: [['this', 'test'], 'Must be this or test.'] }))
@@ -12,7 +12,7 @@ import bodyParser from 'body-parser';
12
12
 
13
13
  // We build a simple express test in order to test the middleware
14
14
  //
15
- var app = express();
15
+ const app = express();
16
16
 
17
17
  app.use(bodyParser.json());
18
18
 
package/test/validate.js CHANGED
@@ -309,7 +309,7 @@ const commonTests = {
309
309
  });
310
310
  });
311
311
  },
312
- all: function(type, validData, invalidData) { var self = this;
312
+ all: function(type, validData, invalidData) { let self = this;
313
313
  ['type', 'required', 'null', 'default', 'equal', 'post'].forEach(function(test) {
314
314
  self[test](type, validData, invalidData);
315
315
  });