From 82e4d8493a0a530cb6196991c9031ea783afa81b Mon Sep 17 00:00:00 2001 From: Gustavo Henke Date: Mon, 23 May 2022 08:40:44 +1000 Subject: [PATCH] 6.14.1 --- package-lock.json | 2 +- package.json | 2 +- website/i18n/en.json | 3 + .../feature-schema-validation.md | 97 +++++++++++++++++++ website/versions.json | 1 + 5 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 website/versioned_docs/version-6.14.1/feature-schema-validation.md diff --git a/package-lock.json b/package-lock.json index 3019f62f..16219d50 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "express-validator", - "version": "6.14.0", + "version": "6.14.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index aebcb3d5..b2f31634 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "Gustavo Henke ", "Federico Ciardi " ], - "version": "6.14.0", + "version": "6.14.1", "homepage": "https://express-validator.github.io", "license": "MIT", "repository": { diff --git a/website/i18n/en.json b/website/i18n/en.json index 050008b2..48c9ecf4 100644 --- a/website/i18n/en.json +++ b/website/i18n/en.json @@ -206,6 +206,9 @@ "version-6.13.0/version-6.13.0-schema-validation": { "title": "Schema Validation" }, + "version-6.14.1/version-6.14.1-schema-validation": { + "title": "Schema Validation" + }, "version-6.2.0/version-6.2.0-validation-chain-api": { "title": "Validation Chain API" }, diff --git a/website/versioned_docs/version-6.14.1/feature-schema-validation.md b/website/versioned_docs/version-6.14.1/feature-schema-validation.md new file mode 100644 index 00000000..b08bdd9f --- /dev/null +++ b/website/versioned_docs/version-6.14.1/feature-schema-validation.md @@ -0,0 +1,97 @@ +--- +id: version-6.14.1-schema-validation +title: Schema Validation +original_id: schema-validation +--- + +Schemas are a special, object-based way of defining validations or sanitizations on requests. +At the root-level, you specify field paths as keys, and objects as values -- which define +the error messages, locations and validations/sanitizations. + +Its syntax looks like this: + +```js +const { checkSchema, validationResult } = require('express-validator'); +app.put( + '/user/:id/password', + checkSchema({ + id: { + // The location of the field, can be one or more of body, cookies, headers, params or query. + // If omitted, all request locations will be checked + in: ['params', 'query'], + errorMessage: 'ID is wrong', + isInt: true, + // Sanitizers can go here as well + toInt: true, + }, + myCustomField: { + // Custom validators + custom: { + options: (value, { req, location, path }) => { + return value + req.body.foo + location + path; + }, + }, + // and sanitizers + customSanitizer: { + options: (value, { req, location, path }) => { + let sanitizedValue; + + if (req.body.foo && location && path) { + sanitizedValue = parseInt(value); + } else { + sanitizedValue = 0; + } + + return sanitizedValue; + }, + }, + }, + password: { + isLength: { + errorMessage: 'Password should be at least 7 chars long', + // Multiple options would be expressed as an array + options: { min: 7 }, + }, + }, + firstName: { + isUppercase: { + // To negate a validator + negated: true, + }, + rtrim: { + // Options as an array + options: [[' ', '-']], + }, + }, + // Support bail functionality in schemas + email: { + isEmail: { + bail: true, + }, + }, + // Support if functionality in schemas + someField: { + isInt: { + if: value => { + return value !== ''; + }, + }, + }, + // Wildcards/dots for nested fields work as well + 'addresses.*.postalCode': { + // Make this field optional when undefined or null + optional: { options: { nullable: true } }, + isPostalCode: { + options: 'US', // set postalCode locale here + }, + }, + }), + (req, res, next) => { + const errors = validationResult(req); + if (!errors.isEmpty()) { + return res.status(400).json({ errors: errors.array() }); + } + // handle the request as usual + }, +); +``` diff --git a/website/versions.json b/website/versions.json index 80acbbe2..96f13f50 100644 --- a/website/versions.json +++ b/website/versions.json @@ -1,4 +1,5 @@ [ + "6.14.1", "6.14.0", "6.13.0", "6.12.2",