From bd188d2bfbe912e3a93b34f8f1f931286d5a3133 Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Fri, 15 Nov 2019 12:20:52 +0100 Subject: [PATCH] Add validation rule of repeatable component to mongo connector --- packages/strapi-connector-mongoose/lib/queries.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/strapi-connector-mongoose/lib/queries.js b/packages/strapi-connector-mongoose/lib/queries.js index bb25df9cf1..9a78277130 100644 --- a/packages/strapi-connector-mongoose/lib/queries.js +++ b/packages/strapi-connector-mongoose/lib/queries.js @@ -406,7 +406,7 @@ const buildSearchOr = (model, query) => { }, []); }; -function validateRepeatableInput(value, { key, min, max }) { +function validateRepeatableInput(value, { key, min, max, required }) { if (!Array.isArray(value)) { const err = new Error(`Component ${key} is repetable. Expected an array`); err.status = 400; @@ -416,20 +416,24 @@ function validateRepeatableInput(value, { key, min, max }) { value.forEach(val => { if (typeof val !== 'object' || Array.isArray(val) || val === null) { const err = new Error( - `Component ${key} as invalid items. Expected each items to be objects` + `Component ${key} has invalid items. Expected each items to be objects` ); err.status = 400; throw err; } }); - if (min && value.length < min) { + if ( + (required === true || (required !== true && value.length > 0)) && + (min && value.length < min) + ) { const err = new Error( `Component ${key} must contain at least ${min} items` ); err.status = 400; throw err; } + if (max && value.length > max) { const err = new Error(`Component ${key} must contain at most ${max} items`); err.status = 400;