strapi/packages/strapi-admin/validation/common-functions/check-fields-are-correctly-nested.js
Pierre Noël 1ee5b7f189 fifth refacto
Signed-off-by: Pierre Noël <petersg83@gmail.com>
2020-07-08 11:10:09 +02:00

25 lines
556 B
JavaScript

'use strict';
const _ = require('lodash');
const checkFieldsAreCorrectlyNested = fields => {
if (_.isNil(fields)) {
// Only check if the fields exist
return true;
} else if (!Array.isArray(fields)) {
return false;
}
let failed = false;
for (let indexA = 0; indexA < fields.length; indexA++) {
failed = fields
.slice(indexA + 1)
.some(fieldB => fieldB.startsWith(fields[indexA]) || fields[indexA].startsWith(fieldB));
if (failed) break;
}
return !failed;
};
module.exports = checkFieldsAreCorrectlyNested;