feat: allow setting non visible fields as defaultSort

This commit is contained in:
Marc-Roig 2023-07-05 15:12:56 +02:00 committed by Gustav Hansen
parent 2514bdc7e9
commit ba8069c1db

View File

@ -1,11 +1,15 @@
'use strict'; 'use strict';
const { yup } = require('@strapi/utils'); const { yup, contentTypes } = require('@strapi/utils');
const { intersection } = require('lodash/fp');
const { getService } = require('../../utils'); const { getService } = require('../../utils');
const { const {
isListable, isListable,
hasEditableAttribute, hasEditableAttribute,
} = require('../../services/utils/configuration/attributes'); } = require('../../services/utils/configuration/attributes');
const { getNonVisibleAttributes, getWritableAttributes } = contentTypes;
/** /**
* Creates the validation schema for content-type configurations * Creates the validation schema for content-type configurations
*/ */
@ -23,6 +27,12 @@ module.exports = (schema, opts = {}) =>
const createSettingsSchema = (schema) => { const createSettingsSchema = (schema) => {
const validAttributes = Object.keys(schema.attributes).filter((key) => isListable(schema, key)); const validAttributes = Object.keys(schema.attributes).filter((key) => isListable(schema, key));
// TODO V5: Refactor non visible fields to be a part of content-manager schema
const model = strapi.getModel(schema.uid);
const nonVisibleAttributes = getNonVisibleAttributes(model);
const writableAttributes = getWritableAttributes(model);
const nonVisibleWritableAttributes = intersection(nonVisibleAttributes, writableAttributes);
return yup return yup
.object() .object()
.shape({ .shape({
@ -33,7 +43,10 @@ const createSettingsSchema = (schema) => {
// should be reset when the type changes // should be reset when the type changes
mainField: yup.string().oneOf(validAttributes.concat('id')).default('id'), mainField: yup.string().oneOf(validAttributes.concat('id')).default('id'),
// should be reset when the type changes // should be reset when the type changes
defaultSortBy: yup.string().oneOf(validAttributes.concat('id')).default('id'), defaultSortBy: yup
.string()
.oneOf(validAttributes.concat(['id', ...nonVisibleWritableAttributes]))
.default('id'),
defaultSortOrder: yup.string().oneOf(['ASC', 'DESC']).default('ASC'), defaultSortOrder: yup.string().oneOf(['ASC', 'DESC']).default('ASC'),
}) })
.noUnknown(); .noUnknown();