js-validation-helper
Version:
a javascript validation helper to easily validate your data
154 lines (125 loc) • 3.91 kB
Markdown
a validation helper to easily validate your data
---
with `npm`
```bash
npm install @baabouj/validator
```
with `yarn`
```bash
yarn add @baabouj/validator
```
with `pnpm`
```bash
pnpm install @baabouj/validator
```
first let’s import the validator from the package :
using ES modules
```js
import validator from "@baabouj/validator";
```
using CommonJs
```js
const validator = require("@baabouj/validator");
```
Now that you imported it, the **validator** provides you with 2 function :
- **`validate`**
used to validate fields within an object
```jsx
const validated = validator.validate(
{
name: "John Doe",
age: 20,
email: "john.doe@gmail.com",
password: "secret",
},
{
name: "required", // with single rule
age: "required|number|gt:18", // with multiple rules
email: ["required", "email"], // with an array of rules
password: [
["required", "Password is required"],
["min:6", "Password must be at least 6 characters long"],
], // with custom error messages
}
);
```
on case of validation didn’t succussed, the _validate_ function returns an object containing each unvalidated field as a key and an array of error messages for that field as a value.
on validation succussed, the _validate_ function returns null.
- **`check`**
used to validate a single field
```jsx
const checked = validator.check(23, "number|gte:18");
// or
const checked = validator.check(23, ["number", "gte:18"]);
// or provide your custom error message
const checked = validator.check(23, [
"number",
["gte:18", "You must be at least 18 years old"],
]);
```
on case of validation didn’t succussed, the _check_ function returns an array of error messages.
on success, the _check_ function returns null.
-
checks if the field is not empty
-
checks if the field is empty
-
checks if the field is a valid email address
-
checks if the field is a valid phone number
-
checks if the field is a valid url
-
checks if the field’s length is equal to the given length
-
checks if the field’s length is greater than the given length
-
checks if the field’s length is less than the given length
-
checks if the field is a number
-
checks if the field is a boolean
-
checks if the field is a string
-
checks if the field is an array
-
checks if the field is an object
-
checks if the field is a function
-
checks if the field is null
-
checks if the field is undefined
-
checks if the field is in the given list
-
checks if the field is not in the given list
-
checks if the field is equal to the given value
-
checks if the field is not equal to the given value
-
checks if the field is greater than the given value
-
checks if the field is greater than or equal to the given value
-
checks if the field is less than the given value
-
checks if the field is less than or equal to the given value
-
checks if the field is between the given values
-
checks if the field contains the given value
-
checks if the field starts with the given value
-
checks if the field ends with the given value
-
checks if the field matches the given regex pattern
-
checks if the field does not match the given rule