Skip to content

Commit 096b5be

Browse files
committed
feat: Added boolean validator
1 parent 03866f9 commit 096b5be

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

__tests__/index.test-d.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import { expectType } from 'tsd';
22

3-
import { nullable, optional } from '../src/modifiers';
4-
import type { TypeOf, Schema } from '../src/types';
5-
import { validate, date, number, oneOf, string, object, array } from '../src/validators';
3+
import {
4+
validate,
5+
date,
6+
number,
7+
oneOf,
8+
string,
9+
object,
10+
array,
11+
boolean,
12+
nullable,
13+
optional,
14+
} from '../src';
15+
import type { TypeOf, Schema } from '../src';
616

717
// Schema
818
declare const str1: TypeOf<Schema<string, {}, never>>;
@@ -55,6 +65,9 @@ expectType<'a' | null>(validate(validator6)(''));
5565
const validator7 = optional(nullable(oneOf(['a'])));
5666
expectType<'a' | null | undefined>(validate(validator7)(''));
5767

68+
const validator8 = optional(nullable(boolean()));
69+
expectType<boolean | null | undefined>(validate(validator8)(false));
70+
5871
// nested
5972
const nested1 = object({});
6073
expectType<{}>(validate(nested1)({}));

__tests__/tests.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Fc from 'fast-check';
44
import { complement, identity, is, lt, prop, __ } from 'ramda';
55
import tsd from 'tsd';
66

7-
import { validate, number, string, date, oneOf, object, array } from '../src';
7+
import { validate, number, string, date, oneOf, object, array, boolean } from '../src';
88

99
const throws = <T extends readonly unknown[]>(predicate: (...args: T) => unknown) => (
1010
...args: T
@@ -60,6 +60,16 @@ describe('@typeofweb/schema', () => {
6060
));
6161
});
6262

63+
describe('boolean', () => {
64+
it('should validate booleans', () =>
65+
Fc.assert(Fc.property(Fc.boolean(), notThrows(validate(boolean())))));
66+
67+
it('should not allow other values', () =>
68+
Fc.assert(
69+
Fc.property(Fc.anything().filter(complement(is(Boolean))), throws(validate(boolean()))),
70+
));
71+
});
72+
6373
describe('date', () => {
6474
it('should validate dates', () =>
6575
Fc.assert(Fc.property(Fc.date(), notThrows(validate(date())))));

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export type { VALIDATORS, ValidatorToType } from './validators';
2-
export { validate, oneOf, string, number, date, array, object } from './validators';
2+
export { validate, oneOf, string, number, boolean, date, array, object } from './validators';
33

44
export type { TypeOf, Schema, AnySchema } from './types';
55
export { optional, nullable } from './modifiers';

src/validators.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const LITERAL_VALIDATOR = Symbol('_literal');
22
const STRING_VALIDATOR = Symbol('string');
33
const NUMBER_VALIDATOR = Symbol('number');
4+
const BOOLEAN_VALIDATOR = Symbol('boolean');
45
const DATE_VALIDATOR = Symbol('Date');
56
export type VALIDATORS =
67
| typeof LITERAL_VALIDATOR
78
| typeof STRING_VALIDATOR
89
| typeof NUMBER_VALIDATOR
10+
| typeof BOOLEAN_VALIDATOR
911
| typeof DATE_VALIDATOR
1012
| Record<keyof any, AnySchema>
1113
| readonly AnySchema[];
@@ -14,6 +16,7 @@ export interface ValidatorToType {
1416
readonly [STRING_VALIDATOR]: string;
1517
readonly [NUMBER_VALIDATOR]: number;
1618
readonly [DATE_VALIDATOR]: Date;
19+
readonly [BOOLEAN_VALIDATOR]: boolean;
1720
}
1821

1922
import { ValidationError } from './errors';
@@ -97,6 +100,11 @@ export const validate = <S extends AnySchema>(schema: S) => (value: unknown): Ty
97100
throw new ValidationError();
98101
}
99102
return value as TypeOf<S>;
103+
case BOOLEAN_VALIDATOR:
104+
if (typeof value !== 'boolean') {
105+
throw new ValidationError();
106+
}
107+
return value as TypeOf<S>;
100108
case DATE_VALIDATOR:
101109
if (Object.prototype.toString.call(value) !== '[object Date]') {
102110
throw new ValidationError();
@@ -135,6 +143,12 @@ export const number = () => {
135143
} as Schema<number, { readonly optional: false; readonly nullable: false }, never>;
136144
};
137145

146+
export const boolean = () => {
147+
return {
148+
__validator: BOOLEAN_VALIDATOR,
149+
} as Schema<boolean, { readonly optional: false; readonly nullable: false }, never>;
150+
};
151+
138152
export const date = () => {
139153
return {
140154
__validator: DATE_VALIDATOR,

0 commit comments

Comments
 (0)