Skip to content

Commit

Permalink
test: add complex test data
Browse files Browse the repository at this point in the history
  • Loading branch information
5alidz committed Apr 4, 2021
1 parent 0462755 commit 2c0c12a
Showing 1 changed file with 108 additions and 2 deletions.
110 changes: 108 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,123 @@
import { createSchema, _ } from '../src/index';
import { TYPEERR } from '../src/constants';
import { DATAERR, TYPEERR } from '../src/constants';

describe('createSchema throws when', () => {
test('passed invalid schema', () => {
// @ts-expect-error
expect(() => createSchema<{ data: null }>(null)).toThrow();
expect(() => createSchema(null)).toThrow();
// @ts-expect-error
expect(() => createSchema(undefined)).toThrow();
// @ts-expect-error
expect(() => createSchema([])).toThrow();
});
});

const Person = createSchema({
is_premium: _.boolean({ optional: true }),
is_verified: _.boolean(),
name: _.string({
maxLength: [24, 'too-long'],
minLength: [2, 'too-short'],
pattern: [/[a-zA-Z ]/g, 'contains-symbols'],
}),
age: _.number({
max: [150, 'too-old'],
min: [13, 'too-young'],
}),
email: _.string({
pattern: [/^[^@]+@[^@]+\.[^@]+$/, 'invalid-email'],
}),
meta: _.record({
id: _.string({ minLength: [1, 'invalid-id'], maxLength: [1000, 'invalid-id'] }),
created: _.number({ is: ['integer', 'timestamp-should-be-intger'] }),
updated: _.number({ optional: true }),
nested: _.record(
{
propA: _.number({ optional: true }),
propB: _.boolean({ optional: true }),
propC: _.string({ optional: true }),
},
{ optional: true }
),
}),
});

describe('validate', () => {
test('test ignores optional', () => {
const errors = Person.validate({
is_verified: true,
name: 'abc',
age: 42,
email: '[email protected]',
meta: {
id: '123',
created: Date.now(),
updated: Date.now(),
},
});
expect(errors).toBe(null);
});
test('emits correct error messages', () => {
const errors = Person.validate(
{
is_premium: 42,
},
true
);
expect(errors).toStrictEqual({ is_premium: 'invalid-type' });
const errors2 = Person.validate(
{
is_premium: 42,
},
true
);
expect(errors2).toStrictEqual({ is_premium: 'invalid-type' });
});
// test('handles eager validation correctly', () => {
// expect(Person.validate({}, true)).toStrictEqual({ name: TYPEERR });
// });
});

describe('produce', () => {
const Person = createSchema({
name: _.string(),
age: _.number(),
email: _.string(),
});

test('throws on first error', () => {
expect(() => Person.produce(null)).toThrow(new TypeError(DATAERR));
expect(() => Person.produce(undefined)).toThrow(new TypeError(DATAERR));
expect(() => Person.produce(34)).toThrow(new TypeError(DATAERR));
expect(() => Person.produce('hello world')).toThrow(new TypeError(DATAERR));
expect(() => {
return Person.produce({ name: 2, age: 42, email: '[email protected]' });
}).toThrow(new TypeError(DATAERR));
});

test('let data throw if it matches the schema', () => {
const p = { name: 'john', age: 42, email: '[email protected]' };
expect(Person.produce(p)).toStrictEqual(p);
});
});

describe('is', () => {
const s = createSchema({
a: _.record({
b: _.string({ optional: true }),
c: _.record({ d: _.number(), e: _.number({ optional: true }) }),
}),
});

test('return correct boolean based on data', () => {
expect(s.is({ a: { c: { d: 42 } } })).toBe(true);
expect(s.is({ a: { b: 'hello', c: { e: 120, d: 42 } } })).toBe(true);

expect(s.is({ a: { b: true, c: { e: 120, d: 42 } } })).toBe(false);
expect(s.is({ a: { c: { d: 'hello' } } })).toBe(false);
});
});

describe('eager validation', () => {
const s = createSchema({
a: _.record({
Expand Down

0 comments on commit 2c0c12a

Please sign in to comment.