https://github.com/selahattinunlu/node-validator
Validator package for node js
https://github.com/selahattinunlu/node-validator
node node-module node-validator nodejs validator
Last synced: 1 day ago
JSON representation
Validator package for node js
- Host: GitHub
- URL: https://github.com/selahattinunlu/node-validator
- Owner: selahattinunlu
- Created: 2019-01-11T22:09:10.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-13T16:24:35.000Z (about 7 years ago)
- Last Synced: 2025-10-11T21:56:27.659Z (6 months ago)
- Topics: node, node-module, node-validator, nodejs, validator
- Language: JavaScript
- Size: 7.81 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
It uses [poppinss/indicative](https://github.com/poppinss/indicative) in under the hood. indicative does not provide any configuration chance for default error messages. And also it does not provide to change attributes. So, I just wrap it and made it configurable.
There was a feature request about it but maintainer of indicative declined it. [(This issue: #59)](https://github.com/poppinss/indicative/issues/59) Maintainer said "you can wrap and configure it". I did it but I don't want to copy the wrapper code in every project. That's why I published it as a npm package.
Finally, I need to say last thing, I like "indicative" package so much. Thanks to maintainer of that package! I hope the maintainer will add configuration to built in feature!
## Installation
```sh
npm install @selahattinunlu/node-validator -S
```
## Usage
### Configuration
You can configure default message and default attributes by passing options parameters while initialize it
```js
const Validator = require('@selahattinunlu/node-validator');
const validator = new Validator({
messages: {
require: '{{ field }} is required.',
email: 'Please enter a valid email',
},
attributes: {
email: 'Email',
}
});
```
or you can set default messages and default attributes using setter functions.
```js
const Validator = require('@selahattinunlu/node-validator');
const validator = new Validator();
validator.setDefaultMessages({
//
});
validator.setDefaultAttributes({
//
});
```
### Validation
```js
const Validator = require('@selahattinunlu/node-validator');
const validator = new Validator();
const exampleData = {
name: 'Selahattin'
};
const options = {
messages: {
required: 'This field is required.',
},
attributes: {
name: 'Name'
}
}
validator
.validate(exampleData, {
name: 'required',
email: 'required|email',
}, options)
.then(() => console.log('everything is okay!'))
.catch(errors => console.log(errors));
```
### Validation Rules
As I said before, this package uses [poppinss/indicative](https://github.com/poppinss/indicative) in under the hood. So you need to check that package's documenatation to learn all rules. Please [click here](https://indicative.adonisjs.com/) to access documentation.