Data Validation
A data validator is a rule or set of rules that is used to ensure that data entered into a field within an object meets specific criteria. Validators are used to validate and control the format and content of data, and can be customized to suit the specific needs of the application or project.
Validators can be used to enforce rules such as data type, length, format, or range, ensuring that data is consistent, accurate, and meaningful. For example, a validator might be used to ensure that a phone number field contains only numeric characters, or that a date field is entered in a specific format.
Validators can be defined for each field within an object, and can be customized to suit the specific needs of the application or project. By using data validators, developers can ensure that data entered into the system is valid and accurate, reducing errors and improving the overall quality of the data.
Here is a list of common validators that can be used across all the fields in the accelerator platform:
isRequired
When set to true, this validator prohibits null or empty string values from being inserted or edited in the field of an object's item. This validator is applicable to all field types.
isUnique
When set to true, the value of the field must be unique in the object's item collection. this validator is applicable for text, number, date, and email fields.
regex
This validator is used to validate text and email fields using a regular expression.
In accelerator platform each field also have various built-in validators. Below is the list
text
isRequired,
isUnique,
regex,
minChars,
maxChars
To restrict the length of characters in a text field use ‘minChars’ and ‘maxChars’ validators.
Sample Field: {
"type": "text",
"name": "name",
"label": "Patient Name",
"validators": {
"isRequired": true,
"minChars": 3,
"maxChars": 50
}
}
number
isRequired,
isUnique,
regex,
min,
max,
isInteger
To only allow integers, set the 'isInteger' to true. To limit the range of values for the number field, use the 'min' and 'max' validators.
Sample Field:
{
"type": "number",
"name": "contactNo",
"label": "Patient Contact No",
"validators": {
"regex": "^[0-9]{10}$",
"isRequired": true
}
} .
date
isRequired,
isUnique,
minDate,
maxDate
To define a date range, use the 'minDate' and 'maxDate' validators, which should be formatted in the same ISO 8601 format. Additionally, it is possible to set either value to 'Today' which will compare the date field value with the date and time when an item is being inserted or edited. For instance, if a date field stores a person's date and time of birth, the 'maxDate' validator can be set to 'Today' to ensure that the date and time of birth is not later than the date and time when the item is being inserted. Sample FIelds: {
"type": "date",
"name": "dob",
"label": "Date of Birth",
"validators": {
"isRequired": true,
"minDate": "1900-12-25",
"maxDate": "Today"
}
}
isRequired,
isUnique,
regex
Sample Field: {
"type": "email",
"name": "email",
"label": "Email",
"validators": {
"isRequired": true,
"isUnique": true,
"regex": "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$"
}
}
choice
isRequired,
isSingleChoice
For all choice fields, it is mandatory to provide the 'options' attribute, which is an array of strings that contains all the available options. To insert multiple values into this field, use a comma-separated string format such as "option1, option2, option3". Additionally, it is possible to apply the ‘isSingleChoice’ validator to only allow one option to be selected.
Sample Field:
{
"type": "choice",
"name": "gender",
"label": "Gender",
"options": [ "Male", "Female" ],
"validators": {
"isRequired": true,
"IsSingleChoice": true
}
}
file
isRequired,
maxFileSize,
minFileSize,
allowedFileTypes
To limit the file size, validators such as "maxFileSize" and "minFileSize" can be used, which accept file sizes in the format of 100.0 KB, 1.0 MB, or 1.0 GB. It’s also possible to allow only a certain types of files using the “allowedFileTypes” validator, accept an array of file extensions
e. g. [ ".jpg", ".png", ".pdf" ].
Sample Field:
{
"type": "file",
"name": "profilePic",
"label": "Upload a Profile Picture",
"validators": {
"isRequired": true,
"minFileSize": "1.5 KB",
"maxFileSize": "1.5 MB",
"allowedFileTypes": [ "jpeg", "png", "jpg" ]
}
}
reference
isRequired
Whenever an Item with a reference field is retrieved, the '_id' allows the retrieval of additional information from the referenced Item. Therefore, the "lookup" attribute is mandatory for reference fields. This attribute is a JSON object that includes the "ObjectId" to hold the Id of the reference object and "returnedFields", an array of fields from the reference object that we want to retrieve when fetching the item. Sample Field:
{
"type": "reference",
"name": "provider",
"label": "Provider",
"validators": {
"isRequired": true
},
"lookup":{
"ObjectId": "<Providers-Object-Id>",
"returnedFields": [ "name", "email" ]
}
} Sample Item: {
"provider": "<Provider-Item-Id>" }
array
isRequired
This Field is used to store JSON Array
Sample Field: {
"type": "array",
"name": "symptoms",
"label": "Symptoms"
}
Sample Item: { "symptoms": [ "Fever", "Cough" ]
}
object
isRequired
This Field is used to store JSON Object Sample Field:
{
"type": "object",
"name": "address",
"label": "Address",
}
Sample Item: { "address": {
"addressLine": "Address Line ", city": "City", …
} }
Last updated