Schema
ForgeForm's schema declaration is at the heart of its validation power. Let's explore various schema examples showcasing different field types, built-in validations, and custom validators (both synchronous and asynchronous), as well as custom regex patterns directly within schemas.
Content
Basic Schema Declaration
Synchronous Custom Validators
Asynchronous Custom Validators
Custom Regex Patterns
Basic Schema Declaration with Various Field Types
This example demonstrates a common schema structure, showcasing various built-in field types and basic validations like required, minLength, maxLength, min, max, and format (for email).
-
In this registrationSchema:
-
We define fields like
username,fullName,email,age,password, etc., each with a specific type (e.g.,'string','email','number','boolean','password'). -
Common validation rules like
required,minLength,maxLength,min,max, andformatErrorMessageare directly specified within each field's definition, making the schema declarative and easy to read. -
Custom error messages (e.g.,
requiredErrorMessage,minLengthErrorMessage) are provided to make error feedback user-friendly.
Using Synchronous Custom Validators:
Synchronous validators are ideal for immediate, client-side validation logic. They are functions that receive the field value and optionally the entire form data. They should return an error message string if validation fails, or undefined if validation passes.
This example demonstrates a synchronous custom validator to ensure that confirmPassword field matches the password field:
-
In passwordMatchSchema:
-
We define a
customValidatorfunction for theconfirmPasswordfield. -
This validator function compares the
confirmPasswordvalue(value)with thepasswordvalue from the entireformData. -
If they don't match, it returns an error message string
'Passwords do not match.'. -
If they match, it returns
undefined, indicating successful validation.
Using Asynchronous Custom Validators:
- Asynchronous validators are used for validations that require operations that might take time, such as checking username availability against a database or calling an API. They are functions that return a
Promisewhich resolves to an error message string (if invalid) orundefined(if valid).
This example shows an asynchronous validator to check if a username is already taken (simulating an API call):
In uniqueUsernameSchema:
- We define an asynchronous function
checkUsernameAvailability(simulating an API call). - The
customValidatorfor theusernamefield is also anasyncfunction. - It calls
checkUsernameAvailability(value)and awaits the result. - If
checkUsernameAvailabilityreturnstrue(username taken), the validator returns an error message. - Otherwise, it returns
undefinedfor successful validation.
Using Custom Regex Patterns in Schema:
While ForgeForm provides over 50 built-in regex patterns through field types like email, url, tel, etc., you can also directly embed custom regular expressions within your schema for more specific format validations.
This example demonstrates using a custom regex to validate a product code format:
In productCodeSchema:
- We use the
patternattribute for theproductCodefield. - The value of
patternis a regular expression literal:/^[A-Z]{3}-\d{4}$/. This regex enforces a format of three uppercase letters, followed by a hyphen, and then four digits. patternErrorMessageprovides a custom error message specifically for regex validation failures.
Combining Validators and Rules
You can freely combine built-in validation rules, synchronous custom validators, asynchronous custom validators, and custom regex patterns within a single schema to create robust and tailored validation logic for your forms. For instance, you can have a field that is required, has a minLength, and also uses a customValidator for more complex business logic.