Skip to content
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

Merged
merged 2 commits into from
Nov 4, 2020

Conversation

mixvar
Copy link
Collaborator

@mixvar mixvar commented Nov 1, 2020

  • export validation rules under validators namespace
  • support early abort of field validation by throwing 'true' (for usage in optional validator)

closes #23

- export validation rules under validators namespace
- support early abort of validationg by throwing 'true'
@mixvar mixvar requested a review from pidkopajo November 1, 2020 17:16
Comment on lines +3 to +23
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 };
Copy link
Collaborator Author

@mixvar mixvar Nov 1, 2020

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
Copy link
Collaborator

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"
}

Copy link
Collaborator Author

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

@mixvar mixvar merged commit 4f52be0 into master Nov 4, 2020
@mixvar mixvar deleted the feat/builtin-validators branch November 4, 2020 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[validation] add built-in validation functions
2 participants