Skip to content

Commit

Permalink
feat: improves required type accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikrut committed Apr 4, 2023
1 parent b6dec7a commit a9cd23a
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/utilities/entityToJSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@ import deepCopyObject from './deepCopyObject';
import { toWords } from './formatLabels';
import { SanitizedConfig } from '../config/types';

const propertyIsRequired = (field: Field) => {
if (fieldAffectsData(field) && (('required' in field && field.required === true))) return true;

const propertyIsOptional = (field: Field) => {
return fieldAffectsData(field) && (('required' in field && field.required === true));
if ('fields' in field) {
if (field.admin?.condition || field.access?.read) return false;
return field.fields.find((subField) => propertyIsRequired(subField));
}

if (field.type === 'tabs') {
return field.tabs.some((tab) => 'name' in tab && tab.fields.find((subField) => propertyIsRequired(subField)));
}

return false;
};

function getCollectionIDType(collections: SanitizedCollectionConfig[], slug: string): 'string' | 'number' {
Expand Down Expand Up @@ -371,7 +381,7 @@ function generateFieldTypes(config: SanitizedConfig, fields: Field[]): {
),
required: [
...fields
.filter(propertyIsOptional)
.filter(propertyIsRequired)
.map((field) => (fieldAffectsData(field) ? field.name : '')),
...requiredTopLevelProps,
],
Expand Down

0 comments on commit a9cd23a

Please sign in to comment.