ajv-error-messages
Advanced tools
Comparing version 1.0.0 to 1.0.1
function normaliseErrorMessages(errors) { | ||
console.log(errors); | ||
var fields = errors.reduce( | ||
function (acc, e) { | ||
acc[e.dataPath.slice(1)] = [e.message.toUpperCase()[0] + e.message.slice(1)]; | ||
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; | ||
@@ -6,0 +11,0 @@ }, |
{ | ||
"name": "ajv-error-messages", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Normalise AJV error messages", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -34,5 +34,6 @@ # ajv-error-messages | ||
var valid = validator(data); | ||
var normalisedErrors; | ||
if (!valid) { | ||
var ajv-errors = validator.errors | ||
var normalisedErrors = normalise(ejv-errors); | ||
var ajv_errors = validator.errors | ||
normalisedErrors = normalise(ajv_errors); | ||
} | ||
@@ -43,3 +44,3 @@ | ||
console.log(JSON.stringify(validatingData(invalid))); | ||
console.log(JSON.stringify(validatingData(invalidData))); | ||
``` | ||
@@ -46,0 +47,0 @@ |
@@ -17,3 +17,3 @@ var test = require('tape'); | ||
type: 'object', | ||
required: ['foo'], | ||
required: ['foo', 'arrayThing'], | ||
additionalProperties: false, | ||
@@ -24,2 +24,11 @@ properties: { | ||
}, | ||
arrayThing: { | ||
type: 'array', | ||
items: [ | ||
{ type: 'string', }, | ||
{ type: 'integer', }, | ||
], | ||
minItems: 2, | ||
maxItems: 2, | ||
}, | ||
}, | ||
@@ -29,2 +38,3 @@ }; | ||
foo: 1, | ||
arrayThing: [123,'123',{}], | ||
}; | ||
@@ -34,2 +44,5 @@ var expectedErrors = { | ||
foo: ['Should be string'], | ||
arrayThing: ['Should NOT have more than 2 items'], | ||
'arrayThing[0]': ['Should be string'], | ||
'arrayThing[1]': ['Should be integer'], | ||
}, | ||
@@ -44,1 +57,31 @@ }; | ||
}); | ||
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 expectedErrors = { | ||
fields: { | ||
'': ['Should NOT have more than 2 items'], | ||
'[0]': ['Should be string'], | ||
'[1]': ['Should 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'); | ||
}); |
5661
90
55
5