Skip to content

Commit 2b67320

Browse files
committed
Fix one with custom error code
1 parent 1c8d999 commit 2b67320

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

lib/one.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function oneVerifyer(tester, tests) {
8686

8787
function createOneReaderWriter(verify, tests, method) {
8888
return (value, options = {}, base = undefined) => {
89-
verify(value);
89+
verify(value, options);
9090
for (const test of tests) {
9191
const readOrWrite = test.verify[method];
9292
if (readOrWrite) {

lib/one.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,52 @@ describe('one', () => {
218218
);
219219
});
220220

221+
it('fails with given error code for reader', () => {
222+
const oneSchema = schema(one(literal(null), object({ bar: integer })));
223+
224+
assert.exception(
225+
() => {
226+
// @ts-expect-error
227+
oneSchema.read({ foo: true }, { error_code: 'INVALID' });
228+
},
229+
{
230+
code: 'INVALID'
231+
}
232+
);
233+
});
234+
235+
it('fails with error code from schema', () => {
236+
const oneSchema = schema(one(literal(null), object({ bar: integer })), {
237+
error_code: 'INVALID'
238+
});
239+
240+
assert.exception(
241+
() => {
242+
// @ts-expect-error
243+
oneSchema({ foo: true });
244+
},
245+
{
246+
code: 'INVALID'
247+
}
248+
);
249+
});
250+
251+
it.skip('fails with error code from schema for reader', () => {
252+
const oneSchema = schema(one(literal(null), object({ bar: integer })), {
253+
error_code: 'INVALID'
254+
});
255+
256+
assert.exception(
257+
() => {
258+
// @ts-expect-error
259+
oneSchema.read({ foo: true });
260+
},
261+
{
262+
code: 'INVALID'
263+
}
264+
);
265+
});
266+
221267
it('passes validator', () => {
222268
const oneSchema = schema(one(literal(null), object({ bar: integer })));
223269

0 commit comments

Comments
 (0)