From be3b6b4e286799e21ffdd85b369b1fc261d378ed Mon Sep 17 00:00:00 2001 From: igorbrasileiro Date: Mon, 9 Sep 2024 09:43:58 -0300 Subject: [PATCH] Fix lint --- packages/utils/src/schema/toIdSchema.ts | 81 ++++----------- packages/utils/src/schema/toPathSchema.ts | 117 +++++++--------------- 2 files changed, 56 insertions(+), 142 deletions(-) diff --git a/packages/utils/src/schema/toIdSchema.ts b/packages/utils/src/schema/toIdSchema.ts index 2f54289311..c18724ce84 100644 --- a/packages/utils/src/schema/toIdSchema.ts +++ b/packages/utils/src/schema/toIdSchema.ts @@ -1,25 +1,11 @@ -import get from "lodash/get"; -import fastDeepEqual from "fast-deep-equal"; +import get from 'lodash/get'; +import fastDeepEqual from 'fast-deep-equal'; -import { - ALL_OF_KEY, - DEPENDENCIES_KEY, - ID_KEY, - ITEMS_KEY, - PROPERTIES_KEY, - REF_KEY, -} from "../constants"; -import isObject from "../isObject"; -import { - FormContextType, - GenericObjectType, - IdSchema, - RJSFSchema, - StrictRJSFSchema, - ValidatorType, -} from "../types"; -import retrieveSchema from "./retrieveSchema"; -import getSchemaType from "../getSchemaType"; +import { ALL_OF_KEY, DEPENDENCIES_KEY, ID_KEY, ITEMS_KEY, PROPERTIES_KEY, REF_KEY } from '../constants'; +import isObject from '../isObject'; +import { FormContextType, GenericObjectType, IdSchema, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types'; +import retrieveSchema from './retrieveSchema'; +import getSchemaType from '../getSchemaType'; /** An internal helper that generates an `IdSchema` object for the `schema`, recursively with protection against * infinite recursion @@ -34,11 +20,7 @@ import getSchemaType from "../getSchemaType"; * @param [_recurseList=[]] - The list of retrieved schemas currently being recursed, used to prevent infinite recursion * @returns - The `IdSchema` object for the `schema` */ -function toIdSchemaInternal< - T = any, - S extends StrictRJSFSchema = RJSFSchema, - F extends FormContextType = any, ->( +function toIdSchemaInternal( validator: ValidatorType, schema: S, idPrefix: string, @@ -46,18 +28,11 @@ function toIdSchemaInternal< id?: string | null, rootSchema?: S, formData?: T, - _recurseList: S[] = [], + _recurseList: S[] = [] ): IdSchema { if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) { - const _schema = retrieveSchema( - validator, - schema, - rootSchema, - formData, - ); - const sameSchemaIndex = _recurseList.findIndex((item) => - fastDeepEqual(item, _schema) - ); + const _schema = retrieveSchema(validator, schema, rootSchema, formData); + const sameSchemaIndex = _recurseList.findIndex((item) => fastDeepEqual(item, _schema)); if (sameSchemaIndex === -1) { return toIdSchemaInternal( validator, @@ -67,7 +42,7 @@ function toIdSchemaInternal< id, rootSchema, formData, - _recurseList.concat(_schema), + _recurseList.concat(_schema) ); } } @@ -80,20 +55,16 @@ function toIdSchemaInternal< id, rootSchema, formData, - _recurseList, + _recurseList ); } const $id = id || idPrefix; const idSchema: IdSchema = { $id } as IdSchema; - if (getSchemaType(schema) === "object" && PROPERTIES_KEY in schema) { + if (getSchemaType(schema) === 'object' && PROPERTIES_KEY in schema) { for (const name in schema.properties) { const field = get(schema, [PROPERTIES_KEY, name]); const fieldId = idSchema[ID_KEY] + idSeparator + name; - (idSchema as IdSchema)[name] = toIdSchemaInternal< - T, - S, - F - >( + (idSchema as IdSchema)[name] = toIdSchemaInternal( validator, isObject(field) ? field : {}, idPrefix, @@ -103,7 +74,7 @@ function toIdSchemaInternal< // 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, [name]), - _recurseList, + _recurseList ); } } @@ -121,26 +92,14 @@ function toIdSchemaInternal< * @param [idSeparator='_'] - The separator to use for the path segments in the id * @returns - The `IdSchema` object for the `schema` */ -export default function toIdSchema< - T = any, - S extends StrictRJSFSchema = RJSFSchema, - F extends FormContextType = any, ->( +export default function toIdSchema( validator: ValidatorType, schema: S, id?: string | null, rootSchema?: S, formData?: T, - idPrefix = "root", - idSeparator = "_", + idPrefix = 'root', + idSeparator = '_' ): IdSchema { - return toIdSchemaInternal( - validator, - schema, - idPrefix, - idSeparator, - id, - rootSchema, - formData, - ); + return toIdSchemaInternal(validator, schema, idPrefix, idSeparator, id, rootSchema, formData); } diff --git a/packages/utils/src/schema/toPathSchema.ts b/packages/utils/src/schema/toPathSchema.ts index a6e0ad077e..720e73683c 100644 --- a/packages/utils/src/schema/toPathSchema.ts +++ b/packages/utils/src/schema/toPathSchema.ts @@ -1,6 +1,6 @@ -import get from "lodash/get"; -import fastDeepEqual from "fast-deep-equal"; -import set from "lodash/set"; +import get from 'lodash/get'; +import fastDeepEqual from 'fast-deep-equal'; +import set from 'lodash/set'; import { ADDITIONAL_PROPERTIES_KEY, @@ -13,18 +13,11 @@ import { PROPERTIES_KEY, REF_KEY, RJSF_ADDITIONAL_PROPERTIES_FLAG, -} from "../constants"; -import getDiscriminatorFieldFromSchema from "../getDiscriminatorFieldFromSchema"; -import { - FormContextType, - GenericObjectType, - PathSchema, - RJSFSchema, - StrictRJSFSchema, - ValidatorType, -} from "../types"; -import getClosestMatchingOption from "./getClosestMatchingOption"; -import retrieveSchema from "./retrieveSchema"; +} from '../constants'; +import getDiscriminatorFieldFromSchema from '../getDiscriminatorFieldFromSchema'; +import { FormContextType, GenericObjectType, PathSchema, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types'; +import getClosestMatchingOption from './getClosestMatchingOption'; +import retrieveSchema from './retrieveSchema'; /** An internal helper that generates an `PathSchema` object for the `schema`, recursively with protection against * infinite recursion @@ -37,28 +30,17 @@ import retrieveSchema from "./retrieveSchema"; * @param [_recurseList=[]] - The list of retrieved schemas currently being recursed, used to prevent infinite recursion * @returns - The `PathSchema` object for the `schema` */ -function toPathSchemaInternal< - T = any, - S extends StrictRJSFSchema = RJSFSchema, - F extends FormContextType = any, ->( +function toPathSchemaInternal( validator: ValidatorType, schema: S, name: string, rootSchema?: S, formData?: T, - _recurseList: S[] = [], + _recurseList: S[] = [] ): PathSchema { if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) { - const _schema = retrieveSchema( - validator, - schema, - rootSchema, - formData, - ); - const sameSchemaIndex = _recurseList.findIndex((item) => - fastDeepEqual(item, _schema) - ); + const _schema = retrieveSchema(validator, schema, rootSchema, formData); + const sameSchemaIndex = _recurseList.findIndex((item) => fastDeepEqual(item, _schema)); if (sameSchemaIndex === -1) { return toPathSchemaInternal( validator, @@ -66,52 +48,32 @@ function toPathSchemaInternal< name, rootSchema, formData, - _recurseList.concat(_schema), + _recurseList.concat(_schema) ); } } let pathSchema: PathSchema = { - [NAME_KEY]: name.replace(/^\./, ""), + [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 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, - ); + const index = getClosestMatchingOption(validator, rootSchema!, formData, xxxOf, 0, discriminator); const _schema: S = xxxOf![index] as S; pathSchema = { ...pathSchema, - ...toPathSchemaInternal( - validator, - _schema, - name, - rootSchema, - formData, - _recurseList, - ), + ...toPathSchemaInternal(validator, _schema, name, rootSchema, formData, _recurseList), }; } - if ( - ADDITIONAL_PROPERTIES_KEY in schema && - schema[ADDITIONAL_PROPERTIES_KEY] !== false - ) { + if (ADDITIONAL_PROPERTIES_KEY in schema && schema[ADDITIONAL_PROPERTIES_KEY] !== false) { set(pathSchema, RJSF_ADDITIONAL_PROPERTIES_FLAG, true); } if (ITEMS_KEY in schema && Array.isArray(formData)) { - const { items: schemaItems, additionalItems: schemaAdditionalItems } = - schema; + const { items: schemaItems, additionalItems: schemaAdditionalItems } = schema; if (Array.isArray(schemaItems)) { formData.forEach((element, i: number) => { @@ -122,7 +84,7 @@ function toPathSchemaInternal< `${name}.${i}`, rootSchema, element, - _recurseList, + _recurseList ); } else if (schemaAdditionalItems) { (pathSchema as PathSchema)[i] = toPathSchemaInternal( @@ -131,12 +93,10 @@ function toPathSchemaInternal< `${name}.${i}`, rootSchema, element, - _recurseList, + _recurseList ); } else { - console.warn( - `Unable to generate path schema for "${name}.${i}". No schema defined for it`, - ); + console.warn(`Unable to generate path schema for "${name}.${i}". No schema defined for it`); } }); } else { @@ -147,24 +107,23 @@ function toPathSchemaInternal< `${name}.${i}`, rootSchema, element, - _recurseList, + _recurseList ); }); } } else if (PROPERTIES_KEY in schema) { for (const property in schema.properties) { const field = get(schema, [PROPERTIES_KEY, property]); - (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, - ); + (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 + ); } } return pathSchema; @@ -179,16 +138,12 @@ function toPathSchemaInternal< * @param [formData] - The current formData, if any, to assist retrieving a schema * @returns - The `PathSchema` object for the `schema` */ -export default function toPathSchema< - T = any, - S extends StrictRJSFSchema = RJSFSchema, - F extends FormContextType = any, ->( +export default function toPathSchema( validator: ValidatorType, schema: S, - name = "", + name = '', rootSchema?: S, - formData?: T, + formData?: T ): PathSchema { return toPathSchemaInternal(validator, schema, name, rootSchema, formData); }