fix: corrects type for required named tab fields (#1939)
* fix: corrects type for required named tab fields * chore: tabs and groups are always required * chore: adjusts tab and group type to omit required since a group/named-tab will always exist
This commit is contained in:
@@ -174,7 +174,7 @@ export type DateField = FieldBase & {
|
||||
}
|
||||
}
|
||||
|
||||
export type GroupField = FieldBase & {
|
||||
export type GroupField = Omit<FieldBase, 'required' | 'validation'> & {
|
||||
type: 'group';
|
||||
fields: Field[];
|
||||
admin?: Admin & {
|
||||
@@ -201,14 +201,14 @@ export type CollapsibleField = Omit<FieldBase, 'name' | 'label'> & {
|
||||
|
||||
export type TabsAdmin = Omit<Admin, 'description'>;
|
||||
|
||||
type TabBase = {
|
||||
type TabBase = Omit<FieldBase, 'required' | 'validation'> & {
|
||||
fields: Field[]
|
||||
description?: Description
|
||||
}
|
||||
|
||||
export type NamedTab = TabBase & FieldBase
|
||||
export type NamedTab = TabBase
|
||||
|
||||
export type UnnamedTab = TabBase & Omit<FieldBase, 'name'> & {
|
||||
export type UnnamedTab = Omit<TabBase, 'name'> & {
|
||||
label: Record<string, string> | string
|
||||
localized?: never
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { FieldAffectingData, fieldAffectsData } from '../../fields/config/types'
|
||||
const isFieldNullable = (field: FieldAffectingData, force: boolean): boolean => {
|
||||
const hasReadAccessControl = field.access && field.access.read;
|
||||
const condition = field.admin && field.admin.condition;
|
||||
return !(force && fieldAffectsData(field) && field.required && !field.localized && !condition && !hasReadAccessControl);
|
||||
return !(force && fieldAffectsData(field) && 'required' in field && field.required && !field.localized && !condition && !hasReadAccessControl);
|
||||
};
|
||||
|
||||
export default isFieldNullable;
|
||||
|
||||
@@ -5,7 +5,7 @@ const withNullableType = (field: FieldAffectingData, type: GraphQLType, forceNul
|
||||
const hasReadAccessControl = field.access && field.access.read;
|
||||
const condition = field.admin && field.admin.condition;
|
||||
|
||||
if (!forceNullable && field.required && !field.localized && !condition && !hasReadAccessControl) {
|
||||
if (!forceNullable && 'required' in field && field.required && !field.localized && !condition && !hasReadAccessControl) {
|
||||
return new GraphQLNonNull(type);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ const withOperators = (field: FieldAffectingData, type: GraphQLType, parentName:
|
||||
const name = `${combineParentName(parentName, field.name)}_operator`;
|
||||
const listOperators = ['in', 'not_in', 'all'];
|
||||
|
||||
if (!field.required) operators.push('exists');
|
||||
if (!('required' in field) || !field.required) operators.push('exists');
|
||||
|
||||
return new GraphQLInputObjectType({
|
||||
name,
|
||||
|
||||
@@ -5,14 +5,13 @@ import { fieldAffectsData, Field, Option, FieldAffectingData, tabHasName } from
|
||||
import { SanitizedCollectionConfig } from '../collections/config/types';
|
||||
import { SanitizedGlobalConfig } from '../globals/config/types';
|
||||
import deepCopyObject from './deepCopyObject';
|
||||
import { groupOrTabHasRequiredSubfield } from './groupOrTabHasRequiredSubfield';
|
||||
import { toWords } from './formatLabels';
|
||||
import { SanitizedConfig } from '../config/types';
|
||||
|
||||
const nonOptionalFieldTypes = ['group', 'array', 'blocks'];
|
||||
|
||||
const propertyIsOptional = (field: Field) => {
|
||||
return fieldAffectsData(field) && (field.required === true || nonOptionalFieldTypes.includes(field.type));
|
||||
return fieldAffectsData(field) && (('required' in field && field.required === true) || nonOptionalFieldTypes.includes(field.type));
|
||||
};
|
||||
|
||||
function getCollectionIDType(collections: SanitizedCollectionConfig[], slug: string): 'string' | 'number' {
|
||||
@@ -310,8 +309,7 @@ function generateFieldTypes(config: SanitizedConfig, fields: Field[]): {
|
||||
case 'tabs': {
|
||||
field.tabs.forEach((tab) => {
|
||||
if (tabHasName(tab)) {
|
||||
const hasRequiredSubfields = groupOrTabHasRequiredSubfield(tab);
|
||||
if (hasRequiredSubfields) requiredTopLevelProps.push(tab.name);
|
||||
requiredTopLevelProps.push(tab.name);
|
||||
|
||||
topLevelProps.push([
|
||||
tab.name,
|
||||
@@ -378,7 +376,7 @@ export function entityToJSONSchema(config: SanitizedConfig, incomingEntity: Sani
|
||||
const idField: FieldAffectingData = { type: 'text', name: 'id', required: true };
|
||||
const customIdField = entity.fields.find((field) => fieldAffectsData(field) && field.name === 'id') as FieldAffectingData;
|
||||
|
||||
if (customIdField) {
|
||||
if (customIdField && customIdField.type !== 'group' && customIdField.type !== 'tab') {
|
||||
customIdField.required = true;
|
||||
} else {
|
||||
entity.fields.unshift(idField);
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Field, fieldAffectsData, Tab } from '../fields/config/types';
|
||||
export const groupOrTabHasRequiredSubfield = (entity: Field | Tab): boolean => {
|
||||
if ('type' in entity && entity.type === 'group') {
|
||||
return entity.fields.some((subField) => {
|
||||
return (fieldAffectsData(subField) && subField.required) || groupOrTabHasRequiredSubfield(subField);
|
||||
return (fieldAffectsData(subField) && 'required' in subField && subField.required) || groupOrTabHasRequiredSubfield(subField);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user