Validators help ensure that user input meets certain criteria or constraints. Below are some examples of validator functions:
Validators
Pattern
Checks if the processed value matches a specific regular expression.
text: Property.LongText({
displayName: 'Text',
required: true,
processors: [],
validators: [Validators.pattern(/^[a-zA-Z0-9]+$/);],
})
Max Length
Verifies if the processed string length is within a specified maximum limit.
text: Property.LongText({
displayName: 'Text',
required: true,
processors: [],
validators: [Validators.maxLength(100)],
})
Min Length
Verifies if the processed string length is within a specified minimum limit.
text: Property.LongText({
displayName: 'Text',
required: true,
validators: [Validators.minLength(41)],
})
Min Value
Ensures that the processed numeric value is greater than or equal to a specified minimum.
age: Property.Number({
displayName: 'Age',
required: true,
validators: [Validators.minValue(100)],
})
Max Value
Ensures that the processed numeric value is less than or equal to a specified maximum.
count: Property.Number({
displayName: 'Count',
required: true,
validators: [Validators.maxValue(7)],
})
In Range
Checks if the processed numeric value falls within a specified range.
score: Property.Number({
displayName: 'Score',
required: true,
validators: [Validators.inRange(0, 100)],
})
One Of
Validates whether the processed value is one of the specified values.
fruit: Property.Text({
displayName: 'Fruit',
required: true,
validators: [Validators.oneOf(["apple", "banana", "orange"])],
})
Number
Validates whether the processed value is a valid number.
quantity: Property.Number({
displayName: 'Quantity',
required: true,
validators: [Validators.number],
})
Non Zero
Validates whether the processed value is strictly NOT zero. Designed to be used with Validators.number
numberToDivideBy: Property.Number({
displayName: 'Divide by',
required: true,
validators: [Validators.number, Validators.nonZero]
})
Image
Verifies whether the processed value is a valid image file based on its extension.
imageFile: Property.File({
displayName: 'Image File',
required: true,
validators: [Validators.image],
})
File
Ensures that the processed value is a valid file.
documentFile: Property.File({
displayName: 'Document File',
required: true,
validators: [Validators.file],
})
Email
Validates whether the processed value is a valid email address.
email: Property.Text({
displayName: 'Email',
required: true,
validators: [Validators.email],
})
URL
Ensures that the processed value is a valid URL.
websiteUrl: Property.Text({
displayName: 'Website URL',
required: true,
validators: [Validators.url],
})
Datetime ISO
Validates whether the processed value is a valid ISO-formatted date and time.
eventDate: Property.DateTime({
displayName: 'Event Date',
required: true,
validators: [Validators.datetimeIso],
})
Custom
Example:
Note: The custom validator allows you to define your own validation logic and error message. It can be used to perform complex validations beyond the provided built-in validators.
const customValidator = {
type: ValidationInputType.STRING,
fn: (property, processedValue, userInput) => {
if (validationFails) {
return "Validation Error: Your custom error message.";
}
return null;
}
};