Skip to content

Commit

Permalink
feat: Add error message for enum. (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeldiRium committed Nov 20, 2020
1 parent dd414ee commit 37664fb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/Value.ts
Expand Up @@ -88,6 +88,15 @@ class Value {
break;
}

case 'enum': {
const { allowedValues } = error.params as Ajv.EnumParams;
const actualValue = failingValue;

message = `No enum match (${actualValue}), expects: ${allowedValues.join(', ')}`;

break;
}

default: {
// Intentionally left blank.
}
Expand Down
17 changes: 17 additions & 0 deletions test/unit/ValueTests.ts
Expand Up @@ -191,6 +191,23 @@ suite('Value', (): void => {
});
}).is.throwing((ex: Error): boolean => ex.message === 'Value 19 is more than maximum 7 (at value.count).');
});

test('throws an error if a string does not match any value of an enum.', async (): Promise<void> => {
schema = new Value({
type: 'object',
properties: {
result: { type: 'string', enum: [ 'succeed', 'fail', 'reject' ]}
},
required: [ 'result' ],
additionalProperties: false
});

assert.that((): void => {
schema.validate({
result: 'invalid-value'
});
}).is.throwing((ex: Error): boolean => ex.message === 'No enum match (invalid-value), expects: succeed, fail, reject (at value.result).');
});
});
});

Expand Down

0 comments on commit 37664fb

Please sign in to comment.