form-request-validation
v1.0.15
Published
A simple and flexible form validation library that allows you to send any request and validate it against customizable field options. Easily define validation rules, patterns, and error handling for various forms and fields.
Downloads
41
Maintainers
Keywords
Readme
Form Request Validation
A simple and flexible form validation library that allows you to send any request and validate it against customizable field options. Easily define validation rules, patterns, and error handling for various forms and fields.
Installation
To install the package, use npm:
npm install form-request-validation
or with yarn:
yarn add form-request-validation
Usage
First, you need to configure your AWS credentials to allow the package to send emails using AWS SES. This can be done by setting up environment variables:
import { validateEmailRequest, LanguageValidationRules, ValidationRules } from 'form-request-validation'
const fieldOptions = {
emailFrom: {
name: 'Email From',
required: true,
validation: true,
minLength: 1,
maxLength: 500,
pattern: /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i,
errorMessage: 'Please provide a valid email address',
},
subject: {
name: 'Subject',
required: false,
validation: true,
pattern: createValidationRegex(LanguageValidationRules.LatinAlphabetPattern, LanguageValidationRules.EstonianPattern, ValidationRules.SpecialCharacterPattern),
errorMessage: 'Please provide a valid subject',
},
}
const defaultFieldOptions = {
subject: {
name: 'Subject',
required: false,
validation: true,
pattern: createValidationRegex(LanguageValidationRules.LatinAlphabetPattern),
errorMessage: 'Please provide a valid subject',
}
}
const formRequest = {
emailFrom: '[email protected]',
subject: 'Contact Us Form Submission',
message: 'This is a test message from the contact form.',
}
const validationErrors = validateEmailRequest(formRequest, fieldOptions, defaultFieldOptions)
if (validationErrors.length > 0) {
throw Error(JSON.stringify(validationErrors))
}
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request or open an issue on GitHub if you find a bug or have a suggestion for improvement.