mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 07:03:38 +00:00
fix bad nesting
Signed-off-by: Pierre Noël <petersg83@gmail.com>
This commit is contained in:
parent
2f388d4bd5
commit
b6a5e78fe3
@ -1,6 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
const { checkFieldsAreCorrectlyNested } = require('../common-functions');
|
||||
const {
|
||||
checkFieldsAreCorrectlyNested,
|
||||
checkFieldsDontHaveDuplicates,
|
||||
} = require('../common-functions');
|
||||
|
||||
describe('Common validation functions', () => {
|
||||
describe('checkFieldsAreCorrectlyNested', () => {
|
||||
@ -16,6 +19,7 @@ describe('Common validation functions', () => {
|
||||
[['name', 'name.firstname'], false],
|
||||
[['name', 'name.firstname.french'], false],
|
||||
[['name.firstname', 'name.firstname.french'], false],
|
||||
[['address', 'addresses'], true],
|
||||
[[], true],
|
||||
[undefined, true],
|
||||
[null, true],
|
||||
@ -28,4 +32,23 @@ describe('Common validation functions', () => {
|
||||
expect(result).toBe(expectedResult);
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkFieldsDontHaveDuplicates', () => {
|
||||
const tests = [
|
||||
[['name'], true],
|
||||
[['name', 'description'], true],
|
||||
[['name', 'description', 'name'], false],
|
||||
[['name.firstname', 'name.lastname'], true],
|
||||
[[], true],
|
||||
[undefined, true],
|
||||
[null, true],
|
||||
['', false],
|
||||
[3, false],
|
||||
];
|
||||
|
||||
test.each(tests)('%p to be %p', (fields, expectedResult) => {
|
||||
const result = checkFieldsDontHaveDuplicates(fields);
|
||||
expect(result).toBe(expectedResult);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -14,7 +14,9 @@ const checkFieldsAreCorrectlyNested = fields => {
|
||||
for (let indexA = 0; indexA < fields.length; indexA++) {
|
||||
failed = fields
|
||||
.slice(indexA + 1)
|
||||
.some(fieldB => fieldB.startsWith(fields[indexA]) || fields[indexA].startsWith(fieldB));
|
||||
.some(
|
||||
fieldB => fieldB.startsWith(`${fields[indexA]}.`) || fields[indexA].startsWith(`${fieldB}.`)
|
||||
);
|
||||
if (failed) break;
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
|
||||
const checkFieldsDontHaveDuplicates = fields => {
|
||||
if (_.isNil(fields)) {
|
||||
// Only check if the fields exist
|
||||
return true;
|
||||
} else if (!Array.isArray(fields)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _.uniq(fields).length === fields.length;
|
||||
};
|
||||
|
||||
module.exports = checkFieldsDontHaveDuplicates;
|
||||
@ -1,5 +1,7 @@
|
||||
const checkFieldsAreCorrectlyNested = require('./check-fields-are-correctly-nested');
|
||||
const checkFieldsDontHaveDuplicates = require('./check-fields-dont-have-duplicates');
|
||||
|
||||
module.exports = {
|
||||
checkFieldsAreCorrectlyNested,
|
||||
checkFieldsDontHaveDuplicates,
|
||||
};
|
||||
|
||||
@ -2,7 +2,10 @@
|
||||
|
||||
const { yup } = require('strapi-utils');
|
||||
const _ = require('lodash');
|
||||
const { checkFieldsAreCorrectlyNested } = require('./common-functions');
|
||||
const {
|
||||
checkFieldsAreCorrectlyNested,
|
||||
checkFieldsDontHaveDuplicates,
|
||||
} = require('./common-functions');
|
||||
|
||||
const email = yup
|
||||
.string()
|
||||
@ -60,8 +63,13 @@ const updatePermissions = yup
|
||||
.nullable()
|
||||
.test(
|
||||
'field-nested',
|
||||
'Fields format are incorrect (duplicates or bad nesting).',
|
||||
'Fields format are incorrect (bad nesting).',
|
||||
checkFieldsAreCorrectlyNested
|
||||
)
|
||||
.test(
|
||||
'field-nested',
|
||||
'Fields format are incorrect (duplicates).',
|
||||
checkFieldsDontHaveDuplicates
|
||||
),
|
||||
conditions: arrayOfConditionNames,
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user