-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(validators): add built-in validation rules #27
Conversation
- export validation rules under validators namespace - support early abort of validationg by throwing 'true'
export type Required = { code: "required" }; | ||
export type OneOf = { code: "oneOf"; allowedValues: Primitive[] }; | ||
|
||
export type Integer = { code: "integer" }; | ||
export type MinValue = { code: "minValue"; min: number }; | ||
export type MaxValue = { code: "maxValue"; max: number }; | ||
export type GreaterThan = { code: "greaterThan"; threshold: number }; | ||
export type LesserThan = { code: "lesserThan"; threshold: number }; | ||
|
||
export type Pattern = { code: "pattern"; regex: RegExp }; | ||
export type HasSpecialChar = { code: "hasSpecialChar" }; | ||
export type HasUpperCaseChar = { code: "hasUpperCaseChar" }; | ||
export type HasLowerCaseChar = { code: "hasLowerCaseChar" }; | ||
|
||
export type MinLength = { code: "minLength"; min: number }; | ||
export type MaxLength = { code: "maxLength"; max: number }; | ||
export type ExactLength = { code: "exactLength"; expected: number }; | ||
|
||
export type ValidDate = { code: "validDate" }; | ||
export type MinDate = { code: "minDate"; min: Date }; | ||
export type MaxDate = { code: "maxDate"; max: Date }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe the error names should be negations of corresponding rules?
I tried that and it was sort of awkward so I am not sure
const notPresent = required()(val) != null; | ||
|
||
if (notPresent) { | ||
throw true; // special way of breaking validation chain early |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elegant hack, but what do you think about allowing validators to return some reserved value to break the validation chain early?
not sure if this feature will be useful, but this case proves it could be, and imho it's not that obvious that throw true
will do the trick (even if documented)
smth like that?
if(notPresent) {
return "break-validation-chain"
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it matters if it's for internal use only. if it needs to be for use by consumers as well then maybe exported Symbol would be good.
I'd leave it for now as internal thing, and if we want to add it as part of public API then we will improve it
optional
validator)closes #23