1.0.5 • Published 2 years ago

@svelidate/validation v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@svelidate/validation

Simple and lightweight validation with no dependencies.

Usage

import { string, validateIf, createValidator } from "@svelidate/validation"

const email = "local-part@domain.extension"
const invalidEmail = "invalid@email"

// validation :
const validator = string.email("Please enter a valid email address.")
validator(email) // returns undefined (no error)
validator(invalidEmail) // returns "Please enter a valid email address."

// conditional validation :
const isEmpty = true
const conditionalValidator = validateIf(() => !isEmpty, validator)
conditionalValidator(invalidEmail) // returns undefined (no error)

// custom validator :
const isString = (value: unknown) => {
	if (typeof value === "string") return undefined // valid
	return "This is not a string !" // invalid (error message)
}
const isNumberFactory = createValidator(value => typeof value === "number")
const isNumber = isNumberFactory("This is not a number !")
isNumber(69) // returns undefined
isNumber(email) // returns "This is not a number !"

Default validators

Svelidate comes with multiple validators that you can use, they are grouped by category (object): general when they can be used for many value types (e.g. required or truthy), string to validate strings, number for numbers and date for dates.

Custom validators

You can also create your own validator, a validator takes the binded input value has an argument and returns undefined if there are no errors or a string containing the error message. Because the error message may change (for example if using translation keys), svelidate provide helper functions to create a validator factory that can take custom error messages. These helper functions take, a callback that must return true if the value is valid or false if it's not, and a string for the default error message (optional).

import {
	createValidator,
	createStringValidator, // will return an error if value is not a string.
	createNumberValidator, // will return an error if value is not a number.
	createDateValidator, // will return an error if value is not a date (it will try to parse it as a date first using the `Date` constructor).
} from "@svelidate/validation"

//
const isObject = createValidator(
	value => typeof value === "object",
	"This is not an object !"
)

const objectValidator = isObject() // this is what you use in form fields (`isObject()`)
objectValidator({}) // return undefined
objectValidator("string") // returns "This is not an object !"

//
// you can also pass params by wrapping it in another function:
const isNumberEqualTo = (number: number) => {
	return createNumberValidator(value => value === number)
}

const threeValidator = isNumberEqualTo(3)("This is not equal to 3 !")
threeValidator(3) // return undefined
threeValidator(69) // return "This is not equal to 3 !"

Conditional validation

You can make any validator or array of validator only validate if a condition is true/undefined by using the validateIf(predicate: Validator | ValidatorPredicate, validators: Validator | Validator[] ) function.

import { validateIf, general } from "@svelidate/validation"

const value = undefined

// if the predicate returns true or undefined the general.required() will be run as normal
validateIf(() => true, general.required("error"))(value) // returns "error"

// else it won't return any errors, even if the value is not valid
validateIf(() => false, general.required("error"))(value) // returns undefined

// validateIf can also be used to validate arrays
validateIf(
	() => false,
	[general.required("error1"), general.truthy("error2")]
).map(validator => validator(value)) // returns [undefined, undefined]

If you want to make a custom validator conditional you can use createConditionalValidator(predicate: Validator | ValidatorPredicate, validator: Validator). Same usage, except it doesn't take arrays.