-
Notifications
You must be signed in to change notification settings - Fork 933
/
boolean.ts
134 lines (116 loc) · 4.17 KB
/
boolean.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import BaseSchema from './schema';
import type { MixedLocale } from './locale';
import type { AnyObject, Maybe, Optionals } from './types';
import type { Defined } from './util/types';
import { boolean as locale } from './locale';
import isAbsent from './util/isAbsent';
export function create() {
return new BooleanSchema();
}
export default class BooleanSchema<
TType extends Maybe<boolean> = boolean | undefined,
TContext extends AnyObject = AnyObject,
TOut extends TType = TType
> extends BaseSchema<TType, TContext, TOut> {
constructor() {
super({ type: 'boolean' });
this.withMutation(() => {
this.transform(function (value) {
if (!this.isType(value)) {
if (/^(true|1)$/i.test(String(value))) return true;
if (/^(false|0)$/i.test(String(value))) return false;
}
return value;
});
});
}
protected _typeCheck(v: any): v is NonNullable<TType> {
if (v instanceof Boolean) v = v.valueOf();
return typeof v === 'boolean';
}
isTrue(
message = locale.isValue,
): BooleanSchema<TType | true, TContext, true | Optionals<TOut>> {
return this.test({
message,
name: 'is-value',
exclusive: true,
params: { value: 'true' },
test(value) {
return isAbsent(value) || value === true;
},
}) as any;
}
isFalse(
message = locale.isValue,
): BooleanSchema<TType | false, TContext, false | Optionals<TOut>> {
return this.test({
message,
name: 'is-value',
exclusive: true,
params: { value: 'false' },
test(value) {
return isAbsent(value) || value === false;
},
}) as any;
}
}
create.prototype = BooleanSchema.prototype;
export default interface BooleanSchema<
TType extends Maybe<boolean>,
TContext extends AnyObject = AnyObject,
TOut extends TType = TType
> extends BaseSchema<TType, TContext, TOut> {
concat<TOther extends BooleanSchema<any, any, any>>(schema: TOther): TOther;
default<TNextDefault extends Maybe<TType>>(
def: TNextDefault | (() => TNextDefault),
): TNextDefault extends undefined
? BooleanSchema<TType | undefined, TContext>
: BooleanSchema<Defined<TType>, TContext>;
defined(msg?: MixedLocale['defined']): DefinedBooleanSchema<TType, TContext>;
required(
msg?: MixedLocale['required'],
): RequiredBooleanSchema<TType, TContext>;
optional(): BooleanSchema<TType, TContext>;
notRequired(): BooleanSchema<TType, TContext>;
nullable(isNullable?: true): BooleanSchema<TType | null>;
nullable(isNullable: false): BooleanSchema<Exclude<TType, null>>;
}
export interface DefinedBooleanSchema<
TType extends Maybe<boolean>,
TContext extends AnyObject = AnyObject
> extends BooleanSchema<TType, TContext, Defined<TType>> {
default<TNextDefault extends Maybe<TType>>(
def: TNextDefault | (() => TNextDefault),
): TNextDefault extends undefined
? BooleanSchema<TType | undefined, TContext>
: BooleanSchema<Defined<TType>, TContext>;
defined(msg?: MixedLocale['defined']): DefinedBooleanSchema<TType, TContext>;
required(
msg?: MixedLocale['required'],
): RequiredBooleanSchema<TType, TContext>;
optional(): BooleanSchema<TType, TContext>;
notRequired(): BooleanSchema<TType, TContext>;
nullable(isNullable?: true): DefinedBooleanSchema<TType | null>;
nullable(isNullable: false): DefinedBooleanSchema<Exclude<TType, null>>;
}
export interface RequiredBooleanSchema<
TType extends Maybe<boolean>,
TContext extends AnyObject = AnyObject
> extends BooleanSchema<TType, TContext, NonNullable<TType>> {
default<TNextDefault extends Maybe<TType>>(
def: TNextDefault | (() => TNextDefault),
): TNextDefault extends undefined
? BooleanSchema<TType | undefined, TContext>
: BooleanSchema<Defined<TType>, TContext>;
defined(msg?: MixedLocale['defined']): DefinedBooleanSchema<TType, TContext>;
required(
msg?: MixedLocale['required'],
): RequiredBooleanSchema<TType, TContext>;
optional(): BooleanSchema<TType, TContext>;
notRequired(): BooleanSchema<TType, TContext>;
nullable(isNullable?: true): RequiredBooleanSchema<TType | null, TContext>;
nullable(
isNullable: false,
): RequiredBooleanSchema<Exclude<TType, null>, TContext>;
}