@@ -15,29 +15,62 @@ import {
15
15
TupleSchema ,
16
16
TupleSchemasToType ,
17
17
ValueSchema ,
18
+ AndSchemasToType ,
19
+ AndSchema ,
18
20
} from "./schemas" ;
19
21
22
+ /**
23
+ * Requires this field to be of type string.
24
+ * @param requiredMessage The error to return if the value is undefined.
25
+ */
20
26
export function string ( requiredMessage ?: string ) : StringSchema {
21
27
return new StringSchema ( requiredMessage ) ;
22
28
}
23
29
30
+ /**
31
+ * Requires this field to be of type string and match `/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/`.
32
+ * @param invalidMessage The error to return if the email is invalid.
33
+ * @param requiredMessage The error to return if the value is undefined.
34
+ */
24
35
export function email ( invalidMessage ?: string , requiredMessage ?: string ) {
25
36
return new StringSchema ( requiredMessage ) . regex ( / ^ [ \w -\. ] + @ ( [ \w - ] + \. ) + [ \w - ] { 2 , 4 } $ / , invalidMessage ) ;
26
37
}
27
38
39
+ /**
40
+ * Requires this field to be of type number.
41
+ * @param requiredMessage The error to return if the value is undefined.
42
+ */
28
43
export function number ( requiredMessage ?: string ) : NumberSchema {
29
44
return new NumberSchema ( requiredMessage ) ;
30
45
}
31
46
47
+ /**
48
+ * Requires this field to be of type boolean.
49
+ * @param requiredMessage The error to return if the value is undefined.
50
+ */
32
51
export function boolean ( requiredMessage ?: string ) : BooleanSchema {
33
52
return new BooleanSchema ( requiredMessage ) ;
34
53
}
35
54
55
+ /**
56
+ * Requires this field to match a value exactly.
57
+ * @param value The required value.
58
+ * @param requiredMessage The error to return if the value is undefined.
59
+ */
36
60
export function value < T extends string | number | boolean | null | undefined > ( value : T , requiredMessage ?: string ) : Schema < T > {
37
61
return new ValueSchema ( value , requiredMessage ) ;
38
62
}
39
63
64
+ /**
65
+ * Requires this field to be a object, matching the specfied keys and their validation schemas
66
+ * @param fields The object and nested validation schemas the value must match.
67
+ * @param requiredMessage The error to return if the value is undefined.
68
+ */
40
69
export function object < T > ( fields : MappedObjectKeySchemas < T > , requiredMessage ?: string ) : MappedObjectSchema < T > ;
70
+ /**
71
+ * Requires this field to be a object, allowing all keys and values.
72
+ * @param requiredMessage The error to return if the value is undefined.
73
+ */
41
74
export function object < T > ( requiredMessage ?: string ) : ObjectSchema ;
42
75
export function object ( ) {
43
76
if ( typeof arguments [ 0 ] === "object" ) {
@@ -47,22 +80,55 @@ export function object() {
47
80
}
48
81
}
49
82
83
+ /**
84
+ * Requires this field to match at least one of the specified validation schemas.
85
+ * @param requiredMessage The error to return if the value is undefined.
86
+ */
50
87
export function or < T extends [ Schema < any > , ...Schema < any > [ ] ] > ( schemas : T , requiredMessage ?: string ) : Schema < OrSchemasToType < T > > {
51
88
return new OrSchema ( schemas , requiredMessage ) ;
52
89
}
53
90
91
+ /**
92
+ * Requires this field to match all of the specified validation schemas.
93
+ * @param requiredMessage The error to return if the value is undefined.
94
+ */
95
+ export function and < T extends [ Schema < any > , ...Schema < any > [ ] ] > ( schemas : T , requiredMessage ?: string ) : Schema < AndSchemasToType < T > > {
96
+ return new AndSchema ( schemas , requiredMessage ) ;
97
+ }
98
+
99
+ /**
100
+ * Requires this field to match a tuple type.
101
+ * @param schemas The tuple and its item's schemas the value must match.
102
+ * @param requiredMessage The error to return if the value is undefined.
103
+ * @returns
104
+ */
54
105
export function tuple < T extends [ Schema < any > , ...Schema < any > [ ] ] > ( schemas : T , requiredMessage ?: string ) : Schema < TupleSchemasToType < T > > {
55
106
return new TupleSchema ( schemas , requiredMessage ) ;
56
107
}
57
108
109
+ /**
110
+ * Requires this field to be an array with each item matching the passed validation schema.
111
+ * @param schema The schema each array item must match.
112
+ * @param requiredMessage The error to return if the value is undefined.
113
+ */
58
114
export function array < T > ( schema : Schema < T > , requiredMessage ?: string ) {
59
115
return new ArraySchema ( schema , requiredMessage ) ;
60
116
}
61
117
118
+ /**
119
+ * Requires this field to match a custom validator. **You should specify the field type by passing a generic parameter: `custom<User>(...)`**
120
+ * @param validator A function that takes a value returns
121
+ * @param requiredMessage The error to return if the value is undefined.
122
+ */
62
123
export function custom < T > ( validator : Validator < T > , requiredMessage ?: string ) {
63
124
return new CustomSchema ( validator , requiredMessage ) ;
64
125
}
65
126
127
+ /**
128
+ * Requires this field to be a date, or a number/string representing a date. It also transforms the number/string value to a Date instance during transformation.
129
+ * @param requiredMessage The error to return if the value is undefined.
130
+ * @param invalidMessage The error to return if the date format is invalid.
131
+ */
66
132
export function date ( requiredMessage ?: string , invalidMessage ?: string ) {
67
133
return new DateSchema ( requiredMessage , invalidMessage ) ;
68
134
}
0 commit comments