Skip to content

Commit

Permalink
feat(n4s): isArrayOf
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent 32fe8a5 commit 516ff54
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 156 deletions.
7 changes: 6 additions & 1 deletion packages/n4s/src/lib/runLazyRule.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';

export default function runLazyRule(
lazyRule: TLazyRuleMethods,
currentValue: any
): TRuleDetailedResult {
return lazyRule.run(currentValue);
try {
return lazyRule.run(currentValue);
} catch {
return ruleReturn.failing();
}
}
115 changes: 115 additions & 0 deletions packages/n4s/src/schema/__tests__/isArrayOf.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import enforce from 'enforce';

describe('enforce.isArrayOf', () => {
describe('lazy interface', () => {
it('Should return a passing return for an empty array', () => {
expect(enforce.isArrayOf(enforce.isString()).run([])).toEqual({
pass: true,
});
});

it('Should return a passing return for valid arrays', () => {
expect(
enforce.isArrayOf(enforce.isString()).run(['a', 'b', 'c'])
).toEqual({ pass: true });

expect(
enforce
.isArrayOf(enforce.anyOf(enforce.isString(), enforce.isNumber()))
.run([1, 'b', 'c'])
).toEqual({ pass: true });

expect(
enforce
.isArrayOf(
enforce.shape({
id: enforce.isNumber(),
username: enforce.isString(),
})
)
.run([
{ id: 1, username: 'b' },
{ id: 2, username: 'c' },
])
).toEqual({ pass: true });
});

it('Should return a failing return for invalid arrays', () => {
expect(enforce.isArrayOf(enforce.isString()).run([1, 2, 3])).toEqual({
pass: false,
});

expect(
enforce
.isArrayOf(enforce.allOf(enforce.isString(), enforce.isNumber()))
.run([1, 2, 3])
).toEqual({
pass: false,
});

expect(
enforce
.isArrayOf(
enforce.shape({
id: enforce.isNumber(),
username: enforce.isString(),
})
)
.run([
{ id: '1', username: 'b' },
{ id: '2', username: 'c' },
{ id: '3', username: 'd' },
])
).toEqual({
pass: false,
});
});
});

describe('eager interface', () => {
it('Should return silently for an empty array', () => {
enforce([]).isArrayOf(enforce.isString());
});

it('Should return silently for valid arrays', () => {
enforce(['a', 'b', 'c']).isArrayOf(enforce.isString());

enforce([1, 'b', 'c']).isArrayOf(
enforce.anyOf(enforce.isString(), enforce.isNumber())
);

enforce([
{ id: 1, username: 'b' },
{ id: 2, username: 'c' },
]).isArrayOf(
enforce.shape({
id: enforce.isNumber(),
username: enforce.isString(),
})
);
});

it('Should throw for invalid arrays', () => {
expect(() => enforce([1, 2, 3]).isArrayOf(enforce.isString())).toThrow();

expect(() =>
enforce([1, 2, 3]).isArrayOf(
enforce.allOf(enforce.isString(), enforce.isNumber())
)
).toThrow();

expect(() =>
enforce([
{ id: '1', username: 'b' },
{ id: '2', username: 'c' },
{ id: '3', username: 'd' },
]).isArrayOf(
enforce.shape({
id: enforce.isNumber(),
username: enforce.isString(),
})
)
).toThrow();
});
});
});
21 changes: 21 additions & 0 deletions packages/n4s/src/schema/__tests__/loose.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import enforce from 'enforce';

describe('enforce.loose for loose matching', () => {
describe('lazy interface', () => {
it('Should return a passing return when value has non-enforced keys', () => {
expect(
enforce
.loose({ username: enforce.isString(), age: enforce.isNumber() })
.run({ username: 'ealush', age: 31, foo: 'bar' })
).toEqual({ pass: true });
});
});
describe('eager interface', () => {
it('Should return sliently return when value has non-enforced keys', () => {
enforce({ username: 'ealush', age: 31, foo: 'bar' }).loose({
username: enforce.isString(),
age: enforce.isNumber(),
});
});
});
});
135 changes: 135 additions & 0 deletions packages/n4s/src/schema/__tests__/shape&loose.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import enforce from 'enforce';

// The base behavior of 'loose' and 'shape' is practically the same
// so we cover them using the same tests.
describe.each(['loose', 'shape'])('enforce.%s', (methodName: string) => {
describe('lazy interface', () => {
it('Should return a passing return when tests are valid', () => {
expect(
enforce[methodName]({
username: enforce.isString(),
age: enforce.isNumber().gt(18),
}).run({ username: 'ealush', age: 31 })
).toEqual({ pass: true });
});

it('Should return a failing return when tests are invalid', () => {
expect(
enforce[methodName]({
username: enforce.isString(),
age: enforce.isNumber().gt(18),
}).run({ username: null, age: 0 })
).toEqual({ pass: false });
});

describe('nested shapes', () => {
it('Should return a passing return when tests are valid', () => {
expect(
enforce[methodName]({
username: enforce.isString(),
age: enforce.isNumber().gt(18),
address: enforce.shape({
street: enforce.isString(),
city: enforce.isString(),
state: enforce.isString(),
zip: enforce.isNumber(),
}),
}).run({
username: 'ealush',
age: 31,
address: {
street: '123 Main St',
city: 'New York',
state: 'NY',
zip: 12345,
},
})
).toEqual({ pass: true });
});
it('Should return a failing return when tests are invalid', () => {
expect(
enforce[methodName]({
username: enforce.isString(),
age: enforce.isNumber().gt(18),
address: enforce.shape({
street: enforce.isString(),
city: enforce.isString(),
state: enforce.isString(),
zip: enforce.isNumber(),
}),
}).run({
username: 'ealush',
age: 31,
address: {
street: '123 Main St',
city: null,
},
})
).toEqual({ pass: false });
});
});
});

describe('eager interface', () => {
it('Should throw an error fora failing return', () => {
expect(() => {
enforce({ username: null, age: 0 })[methodName]({
username: enforce.isString(),
age: enforce.isNumber().gt(18),
});
}).toThrow();
});

it('Should return silently for a passing return', () => {
enforce({ username: 'ealush', age: 31 })[methodName]({
username: enforce.isString(),
age: enforce.isNumber().gt(18),
});
});

describe('nested shapes', () => {
it('Should return silently when tests are valid', () => {
enforce({
username: 'ealush',
age: 31,
address: {
street: '123 Main St',
city: 'New York',
state: 'NY',
zip: 12345,
},
})[methodName]({
username: enforce.isString(),
age: enforce.isNumber().gt(18),
address: enforce.shape({
street: enforce.isString(),
city: enforce.isString(),
state: enforce.isString(),
zip: enforce.isNumber(),
}),
});
});
it('Should throw when tests are invalid', () => {
expect(() => {
enforce({
username: 'ealush',
age: 31,
address: {
street: '123 Main St',
city: null,
},
})[methodName]({
username: enforce.isString(),
age: enforce.isNumber().gt(18),
address: enforce.shape({
street: enforce.isString(),
city: enforce.isString(),
state: enforce.isString(),
zip: enforce.isNumber(),
}),
});
}).toThrow();
});
});
});
});

0 comments on commit 516ff54

Please sign in to comment.