Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sanv92/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.
https://github.com/sanv92/form-request-validation

Last synced: about 1 month ago
JSON representation

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.

Awesome Lists containing this project

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:
```bash
npm install form-request-validation
```

or with yarn:
```bash
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:
```typescript
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.