Skip to content

Commit d5cc782

Browse files
committed
More JSDoc
1 parent cf5424a commit d5cc782

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

src/factory.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,62 @@ import {
1515
TupleSchema,
1616
TupleSchemasToType,
1717
ValueSchema,
18+
AndSchemasToType,
19+
AndSchema,
1820
} from "./schemas";
1921

22+
/**
23+
* Requires this field to be of type string.
24+
* @param requiredMessage The error to return if the value is undefined.
25+
*/
2026
export function string(requiredMessage?: string): StringSchema {
2127
return new StringSchema(requiredMessage);
2228
}
2329

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+
*/
2435
export function email(invalidMessage?: string, requiredMessage?: string) {
2536
return new StringSchema(requiredMessage).regex(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/, invalidMessage);
2637
}
2738

39+
/**
40+
* Requires this field to be of type number.
41+
* @param requiredMessage The error to return if the value is undefined.
42+
*/
2843
export function number(requiredMessage?: string): NumberSchema {
2944
return new NumberSchema(requiredMessage);
3045
}
3146

47+
/**
48+
* Requires this field to be of type boolean.
49+
* @param requiredMessage The error to return if the value is undefined.
50+
*/
3251
export function boolean(requiredMessage?: string): BooleanSchema {
3352
return new BooleanSchema(requiredMessage);
3453
}
3554

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+
*/
3660
export function value<T extends string | number | boolean | null | undefined>(value: T, requiredMessage?: string): Schema<T> {
3761
return new ValueSchema(value, requiredMessage);
3862
}
3963

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+
*/
4069
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+
*/
4174
export function object<T>(requiredMessage?: string): ObjectSchema;
4275
export function object() {
4376
if (typeof arguments[0] === "object") {
@@ -47,22 +80,55 @@ export function object() {
4780
}
4881
}
4982

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+
*/
5087
export function or<T extends [Schema<any>, ...Schema<any>[]]>(schemas: T, requiredMessage?: string): Schema<OrSchemasToType<T>> {
5188
return new OrSchema(schemas, requiredMessage);
5289
}
5390

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+
*/
54105
export function tuple<T extends [Schema<any>, ...Schema<any>[]]>(schemas: T, requiredMessage?: string): Schema<TupleSchemasToType<T>> {
55106
return new TupleSchema(schemas, requiredMessage);
56107
}
57108

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+
*/
58114
export function array<T>(schema: Schema<T>, requiredMessage?: string) {
59115
return new ArraySchema(schema, requiredMessage);
60116
}
61117

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+
*/
62123
export function custom<T>(validator: Validator<T>, requiredMessage?: string) {
63124
return new CustomSchema(validator, requiredMessage);
64125
}
65126

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+
*/
66132
export function date(requiredMessage?: string, invalidMessage?: string) {
67133
return new DateSchema(requiredMessage, invalidMessage);
68134
}

src/schemas.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,28 @@ export class NumberSchema extends SizeSchema<number> {
254254
super(requiredMessage);
255255
}
256256

257+
/**
258+
* Allows this field to have a decimal point. (**All number fields must be integer by default**)
259+
* @param allow True to allow a decimal point, false to not.
260+
* @param mustBeIntegerMessage The error to return if the value has a decimal point while it isn't allowed
261+
*/
257262
public float(allow: boolean = true, mustBeIntegerMessage?: string) {
258263
if (this.allowFloat !== undefined) throw new Error("Duplicate float() call");
259264
this.allowFloat = allow;
260265
if (mustBeIntegerMessage) this.intMessage = mustBeIntegerMessage;
261266
return this;
262267
}
263268

269+
/**
270+
* Rounds this number field during transformation.
271+
* @param rounding The rounding method to apply.
272+
*/
273+
public doRound(rounding: NumberRounding = "floor") {
274+
if (this.rounding) throw new Error("Duplicate doRound() call");
275+
this.rounding = rounding;
276+
return this;
277+
}
278+
264279
public validate(value: unknown, context: ValidationContext = {}) {
265280
let n = super.validateNullable(value);
266281
if (n !== null) return n;
@@ -271,12 +286,6 @@ export class NumberSchema extends SizeSchema<number> {
271286
return super.validateSize(value);
272287
}
273288

274-
public doRound(rounding: NumberRounding = "floor") {
275-
if (this.rounding) throw new Error("Duplicate doRound() call");
276-
this.rounding = rounding;
277-
return this;
278-
}
279-
280289
public transform(value: number, context: TransformationContext) {
281290
switch (this.rounding) {
282291
case "ceil":

0 commit comments

Comments
 (0)