From e5482047860551815ced717e3f775d7519ce5828 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sat, 20 Jan 2024 17:37:38 +0100 Subject: [PATCH] Pass array context --- index.js | 4 +++- package.json | 3 ++- yarn.lock | 12 ++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 26f8231..62770d8 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const combinator = require("@validatem/combinator"); const validationResult = require("@validatem/validation-result"); const annotateErrors = require("@validatem/annotate-errors"); const isArray = require("@validatem/is-array"); +const withContext = require("@validatem/with-context"); module.exports = function (rules) { let validator = combinator((value, applyValidators) => { @@ -11,7 +12,8 @@ module.exports = function (rules) { let allErrors = []; value.forEach((item, i) => { - let { errors, newValue } = applyValidators(item, rules); + let itemRules = withContext(rules, { arrayIndex: i, arrayLength: value.length }); + let { errors, newValue } = applyValidators(item, itemRules); let annotatedErrors = annotateErrors({ pathSegments: [ i ], diff --git a/package.json b/package.json index 201331e..9f65151 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "@validatem/annotate-errors": "^0.1.2", "@validatem/combinator": "^0.1.0", "@validatem/is-array": "^0.1.0", - "@validatem/validation-result": "^0.1.1" + "@validatem/validation-result": "^0.1.1", + "@validatem/with-context": "^0.1.2" } } diff --git a/yarn.lock b/yarn.lock index d4f53e6..644dead 100644 --- a/yarn.lock +++ b/yarn.lock @@ -26,6 +26,11 @@ resolved "https://registry.yarnpkg.com/@validatem/combinator/-/combinator-0.1.0.tgz#d07f49b7c519d7bc0d1060f68ffe162cd80ac6e9" integrity sha512-dW0zCHmeCiZhXjX4A6Til4X4XEcFHclQQ5mDehwRPJwh8rgY/fwuPbUIbuKM0Vgz49KnVxIcCl4xc7p4h6AVRg== +"@validatem/combinator@^0.1.1": + version "0.1.2" + resolved "https://registry.yarnpkg.com/@validatem/combinator/-/combinator-0.1.2.tgz#eab893d55f1643b9c6857eaf6ff7ed2a728e89ff" + integrity sha512-vE8t1tNXknmN62FlN6LxQmA2c6TwVKZ+fl/Wit3H2unFdOhu7SZj2kRPGjAXdK/ARh/3svYfUBeD75pea0j1Sw== + "@validatem/core@^0.3.1": version "0.3.1" resolved "https://registry.yarnpkg.com/@validatem/core/-/core-0.3.1.tgz#33ea24ba4d4c516b5836703aea7e7da0afb6c227" @@ -142,6 +147,13 @@ resolved "https://registry.yarnpkg.com/@validatem/virtual-property/-/virtual-property-0.1.0.tgz#880540dfd149f98ecf1095d93912e34443381fe4" integrity sha512-JUUvWtdqoSkOwlsl20oB3qFHYIL05a/TAfdY4AJcs55QeVTiX5iI1b8IoQW644sIWWooBuLv+XwoxjRsQFczlQ== +"@validatem/with-context@^0.1.2": + version "0.1.2" + resolved "https://registry.yarnpkg.com/@validatem/with-context/-/with-context-0.1.2.tgz#3645c04897664f70995104961277e07b61b4f615" + integrity sha512-noAWf4CsmU+BCz+KOg3GPq9+R9BQLWOQnOgWVfkYHFdLnnbLhl8w/ONdzvFzUYGHIZGKZwsWVCp+Kwz/tAfMnA== + dependencies: + "@validatem/combinator" "^0.1.1" + as-expression@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/as-expression/-/as-expression-1.0.0.tgz#7bc620ca4cb2fe0ee90d86729bd6add33b8fd831"