mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 19:04:38 +00:00
Refactor validators and add better messages
Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu>
This commit is contained in:
parent
df5dc17c75
commit
28beaaa932
@ -46,19 +46,6 @@ const arrayOfConditionNames = yup
|
|||||||
: this.createError({ path: this.path, message: `contains conditions that don't exist` });
|
: this.createError({ path: this.path, message: `contains conditions that don't exist` });
|
||||||
});
|
});
|
||||||
|
|
||||||
const checkContentTypesFieldsRestriction = permissions => {
|
|
||||||
const { actionProvider } = strapi.admin.services.permission;
|
|
||||||
|
|
||||||
if (!_.isArray(permissions)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return permissions.every(({ action: actionId, fields }) => {
|
|
||||||
const action = actionProvider.getByActionId(actionId);
|
|
||||||
return actionDomain.hasFieldsRestriction(action) || _.isNil(fields);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const permissionsAreEquals = (a, b) =>
|
const permissionsAreEquals = (a, b) =>
|
||||||
a.action === b.action && (a.subject === b.subject || (_.isNil(a.subject) && _.isNil(b.subject)));
|
a.action === b.action && (a.subject === b.subject || (_.isNil(a.subject) && _.isNil(b.subject)));
|
||||||
|
|
||||||
@ -93,20 +80,25 @@ const updatePermissions = yup
|
|||||||
'field-nested',
|
'field-nested',
|
||||||
'Fields format are incorrect (duplicates).',
|
'Fields format are incorrect (duplicates).',
|
||||||
checkFieldsDontHaveDuplicates
|
checkFieldsDontHaveDuplicates
|
||||||
|
)
|
||||||
|
.test(
|
||||||
|
'fields-restriction',
|
||||||
|
'The permission at ${path} must have fields set to null or undefined',
|
||||||
|
function(fields) {
|
||||||
|
const { actionProvider } = strapi.admin.services.permission;
|
||||||
|
const action = actionProvider.getByActionId(this.parent.action);
|
||||||
|
|
||||||
|
return actionDomain.hasFieldsRestriction(action) || _.isNil(fields);
|
||||||
|
}
|
||||||
),
|
),
|
||||||
conditions: yup.array().of(yup.string()),
|
conditions: yup.array().of(yup.string()),
|
||||||
})
|
})
|
||||||
.test(
|
|
||||||
'delete-fields-are-null',
|
|
||||||
'Some permissions are duplicated (same action and subject)',
|
|
||||||
checkNoDuplicatedPermissions
|
|
||||||
)
|
|
||||||
.test(
|
|
||||||
'fields-restriction',
|
|
||||||
'The actions must have fields set to null or undefined',
|
|
||||||
checkContentTypesFieldsRestriction
|
|
||||||
)
|
|
||||||
.noUnknown()
|
.noUnknown()
|
||||||
|
)
|
||||||
|
.test(
|
||||||
|
'delete-fields-are-null',
|
||||||
|
'Some permissions are duplicated (same action and subject)',
|
||||||
|
checkNoDuplicatedPermissions
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
.required()
|
.required()
|
||||||
|
|||||||
@ -16,8 +16,8 @@ function isNotNull(msg = '${path} cannot be null.') {
|
|||||||
return this.test('defined', msg, isNotNullTest);
|
return this.test('defined', msg, isNotNullTest);
|
||||||
}
|
}
|
||||||
|
|
||||||
function arrayRequiredAllowEmpty(message) {
|
function arrayRequiredAllowEmpty(message = '${path} is required') {
|
||||||
return this.test('field is required', message || '', value => _.isArray(value));
|
return this.test('field is required', message, value => _.isArray(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
yup.addMethod(yup.mixed, 'notNil', isNotNill);
|
yup.addMethod(yup.mixed, 'notNil', isNotNill);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user