ajv-error-messages
Advanced tools
Comparing version 1.0.3 to 2.0.0
23
index.js
function normaliseErrorMessages(errors) { | ||
var fields = errors.reduce( | ||
function (acc, e) { | ||
if (e.dataPath.length && e.dataPath[0] === '.') { | ||
acc[e.dataPath.slice(1)] = [e.message.toUpperCase()[0] + e.message.slice(1)]; | ||
} else { | ||
acc[e.dataPath] = [e.message.toUpperCase()[0] + e.message.slice(1)]; | ||
} | ||
return acc; | ||
}, | ||
{} | ||
); | ||
var fields = errors.reduce(function (acc, e) { | ||
if (e.instancePath.length && e.instancePath[0] === ".") { | ||
acc[e.instancePath.slice(1)] = [ | ||
e.message.toUpperCase()[0] + e.message.slice(1), | ||
]; | ||
} else { | ||
acc[e.instancePath] = [e.message.toUpperCase()[0] + e.message.slice(1)]; | ||
} | ||
return acc; | ||
}, {}); | ||
return { fields }; | ||
return { fields }; | ||
} | ||
module.exports = normaliseErrorMessages; |
{ | ||
"name": "ajv-error-messages", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "Normalise AJV error messages", | ||
@@ -28,5 +28,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"ajv": "^4.6.0", | ||
"ajv": "^8.10.0", | ||
"tape": "^4.6.0" | ||
} | ||
} |
@@ -51,5 +51,5 @@ # ajv-error-messages | ||
fields: { | ||
'foo': ['Should be string'] | ||
'/foo': ['Must be string'] | ||
} | ||
} | ||
``` |
@@ -1,82 +0,76 @@ | ||
var test = require('tape'); | ||
var normalise = require('../index'); | ||
var SchemaValidator = require('ajv'); | ||
var test = require("tape"); | ||
var normalise = require("../index"); | ||
var SchemaValidator = require("ajv"); | ||
var schemaValidator = new SchemaValidator({ allErrors: true }); | ||
test('normalise should exist', function (t) { | ||
t.plan(1); | ||
t.equal(typeof normalise, 'function', 'normalise is a function'); | ||
test("normalise should exist", function (t) { | ||
t.plan(1); | ||
t.equal(typeof normalise, "function", "normalise is a function"); | ||
}); | ||
test('should normalise errors', function(t) { | ||
t.plan(2); | ||
test("should normalise errors", function (t) { | ||
t.plan(2); | ||
var testSchema = { | ||
description: 'test schema', | ||
type: 'object', | ||
required: ['foo', 'arrayThing'], | ||
additionalProperties: false, | ||
properties: { | ||
foo: { | ||
type: 'string', | ||
}, | ||
arrayThing: { | ||
type: 'array', | ||
items: [ | ||
{ type: 'string', }, | ||
{ type: 'integer', }, | ||
], | ||
minItems: 2, | ||
maxItems: 2, | ||
}, | ||
}, | ||
}; | ||
var testData = { | ||
foo: 1, | ||
arrayThing: [123,'123',{}], | ||
}; | ||
var expectedErrors = { | ||
fields: { | ||
foo: ['Should be string'], | ||
arrayThing: ['Should NOT have more than 2 items'], | ||
'arrayThing[0]': ['Should be string'], | ||
'arrayThing[1]': ['Should be integer'], | ||
}, | ||
}; | ||
var testSchema = { | ||
description: "test schema", | ||
type: "object", | ||
required: ["foo", "arrayThing"], | ||
additionalProperties: false, | ||
properties: { | ||
foo: { | ||
type: "string", | ||
}, | ||
arrayThing: { | ||
type: "array", | ||
items: [{ type: "string" }, { type: "integer" }], | ||
minItems: 2, | ||
maxItems: 2, | ||
}, | ||
}, | ||
}; | ||
var testData = { | ||
foo: 1, | ||
arrayThing: [123, "123", {}], | ||
}; | ||
var expectedErrors = { | ||
fields: { | ||
"/foo": ["Must be string"], | ||
"/arrayThing": ["Must NOT have more than 2 items"], | ||
"/arrayThing/0": ["Must be string"], | ||
"/arrayThing/1": ["Must be integer"], | ||
}, | ||
}; | ||
var validator = schemaValidator.compile(testSchema); | ||
var isValid = validator(testData); | ||
var normalisedErrors = normalise(validator.errors); | ||
t.notOk(isValid, 'validation failed'); | ||
t.deepEqual(normalisedErrors, expectedErrors, 'correctly normalised errors'); | ||
var validator = schemaValidator.compile(testSchema); | ||
var isValid = validator(testData); | ||
var normalisedErrors = normalise(validator.errors); | ||
t.notOk(isValid, "validation failed"); | ||
t.deepEqual(normalisedErrors, expectedErrors, "correctly normalised errors"); | ||
}); | ||
test('should work with root array', function(t) { | ||
t.plan(2); | ||
test("should work with root array", function (t) { | ||
t.plan(2); | ||
var testSchema = { | ||
description: 'test schema', | ||
type: 'array', | ||
items: [ | ||
{ type: 'string', }, | ||
{ type: 'integer', }, | ||
], | ||
minItems: 2, | ||
maxItems: 2, | ||
}; | ||
var testData = [123,'123',{}]; | ||
var testSchema = { | ||
description: "test schema", | ||
type: "array", | ||
items: [{ type: "string" }, { type: "integer" }], | ||
minItems: 2, | ||
maxItems: 2, | ||
}; | ||
var testData = [123, "123", {}]; | ||
var expectedErrors = { | ||
fields: { | ||
'': ['Should NOT have more than 2 items'], | ||
'[0]': ['Should be string'], | ||
'[1]': ['Should be integer'], | ||
}, | ||
}; | ||
var expectedErrors = { | ||
fields: { | ||
"": ["Must NOT have more than 2 items"], | ||
"/0": ["Must be string"], | ||
"/1": ["Must be integer"], | ||
}, | ||
}; | ||
var validator = schemaValidator.compile(testSchema); | ||
var isValid = validator(testData); | ||
var normalisedErrors = normalise(validator.errors); | ||
t.notOk(isValid, 'validation failed'); | ||
t.deepEqual(normalisedErrors, expectedErrors, 'correctly normalised errors'); | ||
var validator = schemaValidator.compile(testSchema); | ||
var isValid = validator(testData); | ||
var normalisedErrors = normalise(validator.errors); | ||
t.notOk(isValid, "validation failed"); | ||
t.deepEqual(normalisedErrors, expectedErrors, "correctly normalised errors"); | ||
}); |
5259
82