Skip to content

Commit

Permalink
fix(schema): validate falsy value
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Sep 20, 2019
1 parent ea2ddff commit 10e5d1c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
54 changes: 54 additions & 0 deletions src/functions/__tests__/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,60 @@ function runSchema(target: any, schemaObj: object) {
}

describe('schema', () => {
describe('validates falsy values such as', () => {
test('empty string', () => {
const testSchema: JSONSchema6 = {
type: 'number',
};

expect(runSchema('', testSchema)).toEqual([
{
message: 'type should be number',
path: [],
},
]);
});

test('zero', () => {
const testSchema: JSONSchema6 = {
type: 'string',
};

expect(runSchema(0, testSchema)).toEqual([
{
message: 'type should be string',
path: [],
},
]);
});

test('false', () => {
const testSchema: JSONSchema6 = {
type: 'string',
};

expect(runSchema(false, testSchema)).toEqual([
{
message: 'type should be string',
path: [],
},
]);
});

test('null', () => {
const testSchema: JSONSchema6 = {
type: 'string',
};

expect(runSchema(null, testSchema)).toEqual([
{
message: 'type should be string',
path: [],
},
]);
});
});

describe('when schema defines unknown format', () => {
const testSchema = {
type: 'string',
Expand Down
2 changes: 1 addition & 1 deletion src/functions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const schema: IFunction<ISchemaOptions> = (targetVal, opts, paths) => {

const path = paths.target || paths.given;

if (!targetVal)
if (targetVal === void 0)
return [
{
path,
Expand Down

0 comments on commit 10e5d1c

Please sign in to comment.