@onedaycat/ts-validator
v1.2.4
Published
Application validator
Downloads
11
Readme
ts-validator
Application validator. Internally uses validator.js to perform validation.
Install
npm install @onedaycat/ts-validator
or
yarn add @onedaycat/ts-validator
Usage
import Validator, { isEmail } from '@onedaycat/ts-validator'
const schema = {
email: [isEmail()],
}
const values = {
email: '[email protected]',
}
// throw error
Validator.test(schema, values)
Example
Main.ts
Customizing the error messages (optional).
import Validator from '@onedaycat/ts-validator'
Validator.message = {
...Validator.message,
min: 'YOUR_MESSAGE'
max: 'YOUR_MESSAGE'
}
SignInInput.ts
import Validator, { isEmail, min } from '@onedaycat/ts-validator'
export default class SignInInput {
constructor(
public email: string,
public password: string,
) {}
public validate() {
const schema = {
email: [isEmail()],
password: [min(10)]
}
const values = {
email: this.email,
password: this.password,
}
// throw error
Validator.test(schema, values)
}
}
Validators
| Validators | Description | |------------|-------------| | min(value: number) | check if the number is less than or equal to minimum. | | max(value: number) | check if the number is more than or equal to maximum. | | isEmail() | check if the string is an email. | | isEmpty() | check if the string has a length of zero. | | isNotEmpty() | check if the string has length more than zero. | | compareWith(field: string) | check if the value matched with the expected field. | | minLength(value: number) | check if the string has length less than or equal to minimum. | | maxLength(value: number) | check if the string has length more than or equal to maximum. |
Contributing
- Fork this repository.
- Create new branch with feature name in format
feature/FEATURE_NAME
- Run
npm install
oryarn
- Create your feature.
- Commit and set commit message with feature name.
- Push your code to your fork repository.
- Create pull request.