validate-form
Simple functional form validation
Example
var Validator = var truthy = var isEmail = var isCreditCard = var range = var match = var memberOf = var list = var validDate = /^\d\d\d\d\/\d\d$/var countries = "US-en" "UK-en" "BR-pt" ...var validate =
Creating your own validators
You can use custom functions as validators. A validator function takes the value to validate as an argument, the key for that value and the parent object that the value is on.
You can use the key to make more readable validation errors and you can use the parent to do validation logic across multiple properties
A validator should either return nothing or an error or an array of errors,
an error in this case is { message: String, type: String }
. The type
is useful if you want to show custom error messages in the UI, then you
can ignore the message and use a custom error message for each type of
validation error.
var Validator = var validate =
Docs
type AlmostValidateError := { type: ValidateErrorType, message: String}type PossibleValidateError = Array<AlmostValidateError> | AlmostValidateError | null type Validator := (value: Any, key: String, parent: Object) => PossibleValidateErrortype ValidateErrorType := "creditCard" | "email" | "length" | "match" | "max" | "memberOf" | "min" | "range" | "truthy" | "type"type ValidateError := { attribute: String, message: String, type: ValidateErrorType} validate-form := (Object<String, Array<Validator>>) => Array<ValidationError> | null validate-form/add-error := (errors: Array<ValidationError>, key: String, maybeError: PossibleValidateError) => Array<ValidationError>
Installation
npm install validate-form
Contributors
- Raynos