From 58f7dd4e0cab4b37e045db11f5109b7c3bcb954f Mon Sep 17 00:00:00 2001 From: Angelin Sirbu Date: Tue, 23 Nov 2021 08:19:12 +0200 Subject: [PATCH 1/3] Updated readme --- README.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9ff8e95..d944417 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,17 @@ 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 and usage](#installation) +2. [Quick example](#quick-example) +3. [Validating both body and parameters](#validating-both-body-and-parameters) +4. [Custom validation output](#custom-validation-output) +5. [Using field values in messages](#using-field-values-in-messages) +6. [Async/await validation](#asyncawait-validation) +7. [Cross field validation](#cross-field-validation) +8. [Contributing](#contributing) + +## Installation > npm i nodejs-schema-validator ## Quick example @@ -146,7 +156,7 @@ const schema = { }; ``` -## Validating with async/await +## Async/await validation Schema rules support async methods. Here's an example: ```js @@ -154,7 +164,7 @@ const schema = { email: { rules: [ { - rule: async (input) => await emailExists(), + rule: async (input) => await emailExists(input), message: 'Email address {{email}} already exists' } ] From 7ea1783c7c1943c51762f582b8249d571bec7ecd Mon Sep 17 00:00:00 2001 From: Angelin Sirbu Date: Tue, 23 Nov 2021 08:31:08 +0200 Subject: [PATCH 2/3] Readme update --- README.md | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d944417..7d5a49b 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,16 @@ 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) # Table of contents -1. [Installation and usage](#installation) +1. [Installation](#installation) 2. [Quick example](#quick-example) -3. [Validating both body and parameters](#validating-both-body-and-parameters) -4. [Custom validation output](#custom-validation-output) -5. [Using field values in messages](#using-field-values-in-messages) -6. [Async/await validation](#asyncawait-validation) -7. [Cross field validation](#cross-field-validation) -8. [Contributing](#contributing) +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 @@ -50,6 +52,7 @@ router.post( ); ``` +## Schema structure Here's a breakdown of how the schema is structured: ```js @@ -79,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 @@ -114,7 +133,7 @@ router.put( ) ``` - +## Validation output Validation failure returns status code 422 with a body in this format: ```js { @@ -127,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 @@ -143,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: { From 5e4432e26a911563aa5e260cd7485488b32aeb1a Mon Sep 17 00:00:00 2001 From: Angelin Sirbu Date: Tue, 23 Nov 2021 08:32:36 +0200 Subject: [PATCH 3/3] Bumped version to 0.10.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 777a2ba..c4c04c8 100644 --- a/package.json +++ b/package.json @@ -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": {