Skip to content

Commit

Permalink
Use Ajv.errorsText instead of custom error message (#79)
Browse files Browse the repository at this point in the history
This provides context to the message you are getting, instead of just saying must be an integer, from now on you will get env/VARIABLE must be an integer.
  • Loading branch information
tommarien committed Aug 3, 2021
1 parent 2ed79c5 commit 38dbbc9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function loadAndValidateEnvironment (_opts) {

const isOptionValid = optsSchemaValidator(opts)
if (!isOptionValid) {
const error = new Error(optsSchemaValidator.errors.map(e => e.message))
const error = new Error(sharedAjvInstance.errorsText(optsSchemaValidator.errors, { dataVar: 'opts' }))
error.errors = optsSchemaValidator.errors
throw error
}
Expand Down Expand Up @@ -97,7 +97,7 @@ function loadAndValidateEnvironment (_opts) {

const valid = ajv.validate(schema, merge)
if (!valid) {
const error = new Error(ajv.errors.map(e => e.message).join('\n'))
const error = new Error(ajv.errorsText(ajv.errors, { dataVar: 'env' }))
error.errors = ajv.errors
throw error
}
Expand Down
10 changes: 4 additions & 6 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ const tests = [
},
data: { },
isOk: false,
errorMessage: 'must have required property \'PORT\''
errorMessage: 'env must have required property \'PORT\''
},
{
name: 'simple object - invalid data',
Expand All @@ -228,7 +228,7 @@ const tests = [
},
data: [],
isOk: false,
errorMessage: 'must NOT have fewer than 1 items,must be object,must match exactly one schema in oneOf'
errorMessage: 'opts/data must NOT have fewer than 1 items, opts/data must be object, opts/data must match exactly one schema in oneOf'
},
{
name: 'simple object - ok - with separator',
Expand Down Expand Up @@ -304,7 +304,7 @@ const tests = [
},
data: {},
isOk: false,
errorMessage: 'must have required property \'ALLOWED_HOSTS\''
errorMessage: 'env must have required property \'ALLOWED_HOSTS\''
},
{
name: 'simple object - KO - multiple required properties',
Expand All @@ -320,9 +320,7 @@ const tests = [
},
data: {},
isOk: false,
errorMessage: `must have required property 'A'
must have required property 'B'
must have required property 'C'`
errorMessage: 'env must have required property \'A\', env must have required property \'B\', env must have required property \'C\''
}
]

Expand Down
6 changes: 3 additions & 3 deletions test/custom-ajv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ const tests = [
},
data: { },
isOk: false,
errorMessage: 'must have required property \'PORT\''
errorMessage: 'env must have required property \'PORT\''
},
{
name: 'simple object - invalid data',
Expand All @@ -229,7 +229,7 @@ const tests = [
},
data: [],
isOk: false,
errorMessage: 'must NOT have fewer than 1 items,must be object,must match exactly one schema in oneOf'
errorMessage: 'opts/data must NOT have fewer than 1 items, opts/data must be object, opts/data must match exactly one schema in oneOf'
}
]

Expand Down Expand Up @@ -269,7 +269,7 @@ const noCoercionTest = {
PORT: '44'
},
isOk: false,
errorMessage: 'must be integer',
errorMessage: 'env/PORT must be integer',
confExpected: {
PORT: 44
}
Expand Down

0 comments on commit 38dbbc9

Please sign in to comment.