Skip to content

Fix one and all with custom error code #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function allVerifyer(tester, tests) {
*/
function createAllReaderWriter(verify, tests, method) {
return (value, options = {}, base = undefined) => {
verify(value);
verify(value, options);
for (const test of tests) {
const readOrWrite = test.verify[method];
if (readOrWrite) {
Expand Down
56 changes: 56 additions & 0 deletions lib/all.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,62 @@ describe('all', () => {
);
});

it('fails with given error code', () => {
const allSchema = schema(all(number.max(-1), number.min(1)));

assert.exception(
() => {
allSchema(42, { error_code: 'INVALID' });
},
{
code: 'INVALID'
}
);
});

it('fails with given error code for reader', () => {
const allSchema = schema(all(number.max(-1), number.min(1)));

assert.exception(
() => {
allSchema.read(42, { error_code: 'INVALID' });
},
{
code: 'INVALID'
}
);
});

it('fails with error code from schema', () => {
const allSchema = schema(all(number.max(-1), number.min(1)), {
error_code: 'INVALID'
});

assert.exception(
() => {
allSchema(42);
},
{
code: 'INVALID'
}
);
});

it.skip('fails with error code from schema for reader', () => {
const allSchema = schema(all(number.max(-1), number.min(1)), {
error_code: 'INVALID'
});

assert.exception(
() => {
allSchema.read(42);
},
{
code: 'INVALID'
}
);
});

it('passes on custom tests', () => {
const allSchema = schema(
all(
Expand Down
2 changes: 1 addition & 1 deletion lib/one.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function oneVerifyer(tester, tests) {

function createOneReaderWriter(verify, tests, method) {
return (value, options = {}, base = undefined) => {
verify(value);
verify(value, options);
for (const test of tests) {
const readOrWrite = test.verify[method];
if (readOrWrite) {
Expand Down
46 changes: 46 additions & 0 deletions lib/one.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,52 @@ describe('one', () => {
);
});

it('fails with given error code for reader', () => {
const oneSchema = schema(one(literal(null), object({ bar: integer })));

assert.exception(
() => {
// @ts-expect-error
oneSchema.read({ foo: true }, { error_code: 'INVALID' });
},
{
code: 'INVALID'
}
);
});

it('fails with error code from schema', () => {
const oneSchema = schema(one(literal(null), object({ bar: integer })), {
error_code: 'INVALID'
});

assert.exception(
() => {
// @ts-expect-error
oneSchema({ foo: true });
},
{
code: 'INVALID'
}
);
});

it.skip('fails with error code from schema for reader', () => {
const oneSchema = schema(one(literal(null), object({ bar: integer })), {
error_code: 'INVALID'
});

assert.exception(
() => {
// @ts-expect-error
oneSchema.read({ foo: true });
},
{
code: 'INVALID'
}
);
});

it('passes validator', () => {
const oneSchema = schema(one(literal(null), object({ bar: integer })));

Expand Down