Skip to content

Commit

Permalink
Merge branch 'release/0.10.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
angi- committed Nov 23, 2021
2 parents 71f974d + 5e4432e commit e5932e9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
39 changes: 34 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ NodeJS validation middleware for express router using schemas for both body and

This library allows you to use any validation library, even your own. Examples are using [validator](https://github.com/validatorjs/validator.js)

# Installation and usage
# Table of contents
1. [Installation](#installation)
2. [Quick example](#quick-example)
3. [Schema structure](#schema-structure)
4. [Optional fields](#optional-fields)
5. [Validating both body and parameters](#validating-both-body-and-parameters)
6. [Validation output](#validation-output)
7. [Using field values in messages](#using-field-values-in-messages)
8. [Async/await validation](#asyncawait-validation)
9. [Cross field validation](#cross-field-validation)
10. [Contributing](#contributing)

## Installation
> npm i nodejs-schema-validator
## Quick example
Expand Down Expand Up @@ -40,6 +52,7 @@ router.post(
);
```

## Schema structure
Here's a breakdown of how the schema is structured:

```js
Expand Down Expand Up @@ -69,6 +82,22 @@ const schemaExample = {
}
}
```

## Optional fields
Marking fields as optional will validate only when they are not empty.
```js
const schema = {
vatNo: {
// Some fields can be optional
optional: true,
rules: [
rule: (input) => !validator.isValidVatNo(input),
message: 'Please insert a valid VAT number of leave empty'
]
}
}
```

## Validating both body and parameters
Example of how to validate both body and url parameters

Expand Down Expand Up @@ -104,7 +133,7 @@ router.put(
)
```


## Validation output
Validation failure returns status code 422 with a body in this format:
```js
{
Expand All @@ -117,7 +146,6 @@ Validation failure returns status code 422 with a body in this format:
}
```

## Custom validation output
In case you want to customize the output and status code of the failure you can pass a function as the second parameter to the middleware. It can be passed to both `paramSchemaValidator` and `bodySchemaValidator`.

```js
Expand All @@ -133,6 +161,7 @@ router.post(
```

## Using field values in messages
Field names wrapped in double curly braces will be replaced with their values. **Please note that there should be no space between the field name and the braces**.
```js
const schema = {
amount: {
Expand All @@ -146,15 +175,15 @@ const schema = {
};
```

## Validating with async/await
## Async/await validation
Schema rules support async methods. Here's an example:

```js
const schema = {
email: {
rules: [
{
rule: async (input) => await emailExists(),
rule: async (input) => await emailExists(input),
message: 'Email address {{email}} already exists'
}
]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodejs-schema-validator",
"version": "0.10.2",
"version": "0.10.3",
"description": "NodeJS validation using schemas",
"main": "./src/schema-validator.js",
"scripts": {
Expand Down

0 comments on commit e5932e9

Please sign in to comment.