import get from 'lodash/get'; import isObject from 'lodash/isObject'; import set from 'lodash/set'; import { ADDITIONAL_PROPERTIES_KEY, ALL_OF_KEY, ANY_OF_KEY, DEPENDENCIES_KEY, IF_KEY, ITEMS_KEY, NAME_KEY, ONE_OF_KEY, PROPERTIES_KEY, REF_KEY, RJSF_ADDITIONAL_PROPERTIES_FLAG, } from '../constants'; import getDiscriminatorFieldFromSchema from '../getDiscriminatorFieldFromSchema'; import { Experimental_CustomMergeAllOf, FormContextType, GenericObjectType, PathSchema, RJSFSchema, StrictRJSFSchema, ValidatorType, } from '../types'; import getClosestMatchingOption from './getClosestMatchingOption'; import retrieveSchema from './retrieveSchema'; import deepEquals from '../deepEquals'; /** An internal helper that generates an `PathSchema` object for the `schema`, recursively with protection against * infinite recursion * * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary * @param schema - The schema for which the `PathSchema` is desired * @param [name=''] - The base name for the schema * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s * @param [formData] - The current formData, if any, to assist retrieving a schema * @param [_recurseList=[]] - The list of retrieved schemas currently being recursed, used to prevent infinite recursion * @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas * @returns - The `PathSchema` object for the `schema` */ function toPathSchemaInternal( validator: ValidatorType, schema: S, name: string, rootSchema?: S, formData?: T, _recurseList: S[] = [], experimental_customMergeAllOf?: Experimental_CustomMergeAllOf, ): PathSchema { if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema || IF_KEY in schema) { const _schema = retrieveSchema(validator, schema, rootSchema, formData, experimental_customMergeAllOf); const sameSchemaIndex = _recurseList.findIndex((item) => deepEquals(item, _schema)); if (sameSchemaIndex === -1) { return toPathSchemaInternal( validator, _schema, name, rootSchema, formData, _recurseList.concat(_schema), experimental_customMergeAllOf, ); } } let pathSchema: PathSchema = { [NAME_KEY]: name.replace(/^\./, ''), } as PathSchema; if (ONE_OF_KEY in schema || ANY_OF_KEY in schema) { const xxxOf: S[] = ONE_OF_KEY in schema ? (schema.oneOf as S[]) : (schema.anyOf as S[]); const discriminator = getDiscriminatorFieldFromSchema(schema); const index = getClosestMatchingOption( validator, rootSchema!, formData, xxxOf, 0, discriminator, experimental_customMergeAllOf, ); const _schema: S = xxxOf![index] as S; pathSchema = { ...pathSchema, ...toPathSchemaInternal( validator, _schema, name, rootSchema, formData, _recurseList, experimental_customMergeAllOf, ), }; } if (ADDITIONAL_PROPERTIES_KEY in schema && schema[ADDITIONAL_PROPERTIES_KEY] !== false) { set(pathSchema, RJSF_ADDITIONAL_PROPERTIES_FLAG, true); const additionalSchema = ( isObject(schema[ADDITIONAL_PROPERTIES_KEY]) ? schema[ADDITIONAL_PROPERTIES_KEY] : {} ) as S; const definedProperties = get(schema, PROPERTIES_KEY, {}); for (const key of Object.keys((formData ?? {}) as GenericObjectType)) { if (!(key in definedProperties)) { (pathSchema as PathSchema)[key] = toPathSchemaInternal( validator, additionalSchema, `${name}.${key}`, rootSchema, get(formData, [key]), _recurseList, experimental_customMergeAllOf, ); } } } if (ITEMS_KEY in schema && Array.isArray(formData)) { const { items: schemaItems, additionalItems: schemaAdditionalItems } = schema; if (Array.isArray(schemaItems)) { formData.forEach((element, i: number) => { if (schemaItems[i]) { (pathSchema as PathSchema)[i] = toPathSchemaInternal( validator, schemaItems[i] as S, `${name}.${i}`, rootSchema, element, _recurseList, experimental_customMergeAllOf, ); } else if (schemaAdditionalItems) { (pathSchema as PathSchema)[i] = toPathSchemaInternal( validator, schemaAdditionalItems as S, `${name}.${i}`, rootSchema, element, _recurseList, experimental_customMergeAllOf, ); } else { console.warn(`Unable to generate path schema for "${name}.${i}". No schema defined for it`); } }); } else { formData.forEach((element, i: number) => { (pathSchema as PathSchema)[i] = toPathSchemaInternal( validator, schemaItems as S, `${name}.${i}`, rootSchema, element, _recurseList, experimental_customMergeAllOf, ); }); } } else if (PROPERTIES_KEY in schema) { for (const property in schema.properties) { const field: S = get(schema, [PROPERTIES_KEY, property], {}) as S; (pathSchema as PathSchema)[property] = toPathSchemaInternal( validator, field, `${name}.${property}`, rootSchema, // It's possible that formData is not an object -- this can happen if an // array item has just been added, but not populated with data yet get(formData, [property]), _recurseList, experimental_customMergeAllOf, ); } } return pathSchema; } /** Generates an `PathSchema` object for the `schema`, recursively * * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary * @param schema - The schema for which the `PathSchema` is desired * @param [name=''] - The base name for the schema * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s * @param [formData] - The current formData, if any, to assist retrieving a schema * @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas * @returns - The `PathSchema` object for the `schema` */ export default function toPathSchema( validator: ValidatorType, schema: S, name = '', rootSchema?: S, formData?: T, experimental_customMergeAllOf?: Experimental_CustomMergeAllOf, ): PathSchema { return toPathSchemaInternal(validator, schema, name, rootSchema, formData, undefined, experimental_customMergeAllOf); }