Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbrasileiro committed Sep 9, 2024
1 parent c69d0a6 commit be3b6b4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 142 deletions.
81 changes: 20 additions & 61 deletions packages/utils/src/schema/toIdSchema.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -34,30 +20,19 @@ 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<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
validator: ValidatorType<T, S, F>,
schema: S,
idPrefix: string,
idSeparator: string,
id?: string | null,
rootSchema?: S,
formData?: T,
_recurseList: S[] = [],
_recurseList: S[] = []
): IdSchema<T> {
if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
const _schema = retrieveSchema<T, S, F>(
validator,
schema,
rootSchema,
formData,
);
const sameSchemaIndex = _recurseList.findIndex((item) =>
fastDeepEqual(item, _schema)
);
const _schema = retrieveSchema<T, S, F>(validator, schema, rootSchema, formData);
const sameSchemaIndex = _recurseList.findIndex((item) => fastDeepEqual(item, _schema));
if (sameSchemaIndex === -1) {
return toIdSchemaInternal<T, S, F>(
validator,
Expand All @@ -67,7 +42,7 @@ function toIdSchemaInternal<
id,
rootSchema,
formData,
_recurseList.concat(_schema),
_recurseList.concat(_schema)
);
}
}
Expand All @@ -80,20 +55,16 @@ function toIdSchemaInternal<
id,
rootSchema,
formData,
_recurseList,
_recurseList
);
}
const $id = id || idPrefix;
const idSchema: IdSchema<T> = { $id } as IdSchema<T>;
if (getSchemaType<S>(schema) === "object" && PROPERTIES_KEY in schema) {
if (getSchemaType<S>(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<GenericObjectType>)[name] = toIdSchemaInternal<
T,
S,
F
>(
(idSchema as IdSchema<GenericObjectType>)[name] = toIdSchemaInternal<T, S, F>(
validator,
isObject(field) ? field : {},
idPrefix,
Expand All @@ -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
);
}
}
Expand All @@ -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<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
validator: ValidatorType<T, S, F>,
schema: S,
id?: string | null,
rootSchema?: S,
formData?: T,
idPrefix = "root",
idSeparator = "_",
idPrefix = 'root',
idSeparator = '_'
): IdSchema<T> {
return toIdSchemaInternal<T, S, F>(
validator,
schema,
idPrefix,
idSeparator,
id,
rootSchema,
formData,
);
return toIdSchemaInternal<T, S, F>(validator, schema, idPrefix, idSeparator, id, rootSchema, formData);
}
117 changes: 36 additions & 81 deletions packages/utils/src/schema/toPathSchema.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -37,81 +30,50 @@ 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<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
validator: ValidatorType<T, S, F>,
schema: S,
name: string,
rootSchema?: S,
formData?: T,
_recurseList: S[] = [],
_recurseList: S[] = []
): PathSchema<T> {
if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
const _schema = retrieveSchema<T, S, F>(
validator,
schema,
rootSchema,
formData,
);
const sameSchemaIndex = _recurseList.findIndex((item) =>
fastDeepEqual(item, _schema)
);
const _schema = retrieveSchema<T, S, F>(validator, schema, rootSchema, formData);
const sameSchemaIndex = _recurseList.findIndex((item) => fastDeepEqual(item, _schema));
if (sameSchemaIndex === -1) {
return toPathSchemaInternal<T, S, F>(
validator,
_schema,
name,
rootSchema,
formData,
_recurseList.concat(_schema),
_recurseList.concat(_schema)
);
}
}

let pathSchema: PathSchema<T> = {
[NAME_KEY]: name.replace(/^\./, ""),
[NAME_KEY]: name.replace(/^\./, ''),
} as PathSchema<T>;

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<S>(schema);
const index = getClosestMatchingOption<T, S, F>(
validator,
rootSchema!,
formData,
xxxOf,
0,
discriminator,
);
const index = getClosestMatchingOption<T, S, F>(validator, rootSchema!, formData, xxxOf, 0, discriminator);
const _schema: S = xxxOf![index] as S;
pathSchema = {
...pathSchema,
...toPathSchemaInternal<T, S, F>(
validator,
_schema,
name,
rootSchema,
formData,
_recurseList,
),
...toPathSchemaInternal<T, S, F>(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) => {
Expand All @@ -122,7 +84,7 @@ function toPathSchemaInternal<
`${name}.${i}`,
rootSchema,
element,
_recurseList,
_recurseList
);
} else if (schemaAdditionalItems) {
(pathSchema as PathSchema<T[]>)[i] = toPathSchemaInternal<T, S, F>(
Expand All @@ -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 {
Expand All @@ -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<GenericObjectType>)[property] =
toPathSchemaInternal<T, S, F>(
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<GenericObjectType>)[property] = toPathSchemaInternal<T, S, F>(
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;
Expand All @@ -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<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
validator: ValidatorType<T, S, F>,
schema: S,
name = "",
name = '',
rootSchema?: S,
formData?: T,
formData?: T
): PathSchema<T> {
return toPathSchemaInternal(validator, schema, name, rootSchema, formData);
}

0 comments on commit be3b6b4

Please sign in to comment.