Skip to content

Commit

Permalink
Fix error paths for array elements #6
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche committed Aug 24, 2020
1 parent cc8d381 commit 90a0e16
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const buildFlatAjvValidate = (ajv, schema, ajvTranslate) => {
export const translateAjv = ({dataPath, keyword, params, message}) => {
let fieldName = dataPath.substr(0,1) === '.' ? dataPath.substr(1) : dataPath

fieldName = fieldName.replace(/\[/g, '.').replace(/\]/g, '')

if (keyword === 'required') {
fieldName = (fieldName ? fieldName + '.' : '') + params.missingProperty
}
Expand Down
12 changes: 12 additions & 0 deletions test/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ describe('Ajv', () => {
expect(validator({_: {foo: {bar: 123}}})).toEqual({'foo.bar': ['Type']})
})

it('Validate type on array element', () => {
let schema = {type: 'array', items: {type: 'string'}}
let validator = buildFlatAjvValidate(undefined, schema)

// expect(validator({_: ['foo', 123]})).toEqual({'1': ['Type']})

schema = {type: 'object', properties: {'foo': {type: 'array', items: {type: 'string'}}}}
validator = buildFlatAjvValidate(undefined, schema)

expect(validator({_: {foo: ['foo', 123]}})).toEqual({'foo.1': ['Type']})
})

it('Validate required property', () => {
let schema = {type: 'object', properties: {'foo': true}, required: ['foo']}
let validator = buildFlatAjvValidate(undefined, schema)
Expand Down

0 comments on commit 90a0e16

Please sign in to comment.