Skip to content

Commit

Permalink
run cs-format
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbrasileiro committed Sep 9, 2024
1 parent a9e2e93 commit faf212f
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 644 deletions.
146 changes: 31 additions & 115 deletions packages/utils/src/createSchemaUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import deepEquals from "./deepEquals";
import deepEquals from './deepEquals';
import {
ErrorSchema,
Experimental_DefaultFormStateBehavior,
Expand All @@ -12,7 +12,7 @@ import {
UiSchema,
ValidationData,
ValidatorType,
} from "./types";
} from './types';
import {
getClosestMatchingOption,
getDefaultFormState,
Expand All @@ -27,18 +27,16 @@ import {
sanitizeDataForNewSchema,
toIdSchema,
toPathSchema,
} from "./schema";
} from './schema';

/** The `SchemaUtils` class provides a wrapper around the publicly exported APIs in the `utils/schema` directory such
* that one does not have to explicitly pass the `validator`, `rootSchema`, or `experimental_defaultFormStateBehavior` to each method.
* Since these generally do not change across a `Form`, this allows for providing a simplified set of APIs to the
* `@rjsf/core` components and the various themes as well. This class implements the `SchemaUtilsType` interface.
*/
class SchemaUtils<
T = any,
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any,
> implements SchemaUtilsType<T, S, F> {
class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>
implements SchemaUtilsType<T, S, F>
{
rootSchema: S;
validator: ValidatorType<T, S, F>;
experimental_defaultFormStateBehavior: Experimental_DefaultFormStateBehavior;
Expand All @@ -52,13 +50,11 @@ class SchemaUtils<
constructor(
validator: ValidatorType<T, S, F>,
rootSchema: S,
experimental_defaultFormStateBehavior:
Experimental_DefaultFormStateBehavior,
experimental_defaultFormStateBehavior: Experimental_DefaultFormStateBehavior
) {
this.rootSchema = rootSchema;
this.validator = validator;
this.experimental_defaultFormStateBehavior =
experimental_defaultFormStateBehavior;
this.experimental_defaultFormStateBehavior = experimental_defaultFormStateBehavior;
}

/** Returns the `ValidatorType` in the `SchemaUtilsType`
Expand All @@ -81,18 +77,15 @@ class SchemaUtils<
doesSchemaUtilsDiffer(
validator: ValidatorType<T, S, F>,
rootSchema: S,
experimental_defaultFormStateBehavior = {},
experimental_defaultFormStateBehavior = {}
): boolean {
if (!validator || !rootSchema) {
return false;
}
return (
this.validator !== validator ||
!deepEquals(this.rootSchema, rootSchema) ||
!deepEquals(
this.experimental_defaultFormStateBehavior,
experimental_defaultFormStateBehavior,
)
!deepEquals(this.experimental_defaultFormStateBehavior, experimental_defaultFormStateBehavior)
);
}

Expand All @@ -109,15 +102,15 @@ class SchemaUtils<
getDefaultFormState(
schema: S,
formData?: T,
includeUndefinedValues: boolean | "excludeObjectChildren" = false,
includeUndefinedValues: boolean | 'excludeObjectChildren' = false
): T | T[] | undefined {
return getDefaultFormState<T, S, F>(
this.validator,
schema,
formData,
this.rootSchema,
includeUndefinedValues,
this.experimental_defaultFormStateBehavior,
this.experimental_defaultFormStateBehavior
);
}

Expand All @@ -129,18 +122,8 @@ class SchemaUtils<
* @param [globalOptions={}] - The optional Global UI Schema from which to get any fallback `xxx` options
* @returns - True if the label should be displayed or false if it should not
*/
getDisplayLabel(
schema: S,
uiSchema?: UiSchema<T, S, F>,
globalOptions?: GlobalUISchemaOptions,
) {
return getDisplayLabel<T, S, F>(
this.validator,
schema,
uiSchema,
this.rootSchema,
globalOptions,
);
getDisplayLabel(schema: S, uiSchema?: UiSchema<T, S, F>, globalOptions?: GlobalUISchemaOptions) {
return getDisplayLabel<T, S, F>(this.validator, schema, uiSchema, this.rootSchema, globalOptions);
}

/** Determines which of the given `options` provided most closely matches the `formData`.
Expand All @@ -160,15 +143,15 @@ class SchemaUtils<
formData: T | undefined,
options: S[],
selectedOption?: number,
discriminatorField?: string,
discriminatorField?: string
): number {
return getClosestMatchingOption<T, S, F>(
this.validator,
this.rootSchema,
formData,
options,
selectedOption,
discriminatorField,
discriminatorField
);
}

Expand All @@ -181,18 +164,8 @@ class SchemaUtils<
* determine which option is selected
* @returns - The firstindex of the matched option or 0 if none is available
*/
getFirstMatchingOption(
formData: T | undefined,
options: S[],
discriminatorField?: string,
): number {
return getFirstMatchingOption<T, S, F>(
this.validator,
formData,
options,
this.rootSchema,
discriminatorField,
);
getFirstMatchingOption(formData: T | undefined, options: S[], discriminatorField?: string): number {
return getFirstMatchingOption<T, S, F>(this.validator, formData, options, this.rootSchema, discriminatorField);
}

/** Given the `formData` and list of `options`, attempts to find the index of the option that best matches the data.
Expand All @@ -205,18 +178,8 @@ class SchemaUtils<
* @returns - The index of the matched option or 0 if none is available
* @deprecated
*/
getMatchingOption(
formData: T | undefined,
options: S[],
discriminatorField?: string,
) {
return getMatchingOption<T, S, F>(
this.validator,
formData,
options,
this.rootSchema,
discriminatorField,
);
getMatchingOption(formData: T | undefined, options: S[], discriminatorField?: string) {
return getMatchingOption<T, S, F>(this.validator, formData, options, this.rootSchema, discriminatorField);
}

/** Checks to see if the `schema` and `uiSchema` combination represents an array of files
Expand All @@ -226,12 +189,7 @@ class SchemaUtils<
* @returns - True if schema/uiSchema contains an array of files, otherwise false
*/
isFilesArray(schema: S, uiSchema?: UiSchema<T, S, F>) {
return isFilesArray<T, S, F>(
this.validator,
schema,
uiSchema,
this.rootSchema,
);
return isFilesArray<T, S, F>(this.validator, schema, uiSchema, this.rootSchema);
}

/** Checks to see if the `schema` combination represents a multi-select
Expand Down Expand Up @@ -263,15 +221,8 @@ class SchemaUtils<
* @deprecated - Use the `validationDataMerge()` function exported from `@rjsf/utils` instead. This function will be
* removed in the next major release.
*/
mergeValidationData(
validationData: ValidationData<T>,
additionalErrorSchema?: ErrorSchema<T>,
): ValidationData<T> {
return mergeValidationData<T, S, F>(
this.validator,
validationData,
additionalErrorSchema,
);
mergeValidationData(validationData: ValidationData<T>, additionalErrorSchema?: ErrorSchema<T>): ValidationData<T> {
return mergeValidationData<T, S, F>(this.validator, validationData, additionalErrorSchema);
}

/** Retrieves an expanded schema that has had all of its conditions, additional properties, references and
Expand All @@ -283,12 +234,7 @@ class SchemaUtils<
* @returns - The schema having its conditions, additional properties, references and dependencies resolved
*/
retrieveSchema(schema: S, rawFormData?: T) {
return retrieveSchema<T, S, F>(
this.validator,
schema,
this.rootSchema,
rawFormData,
);
return retrieveSchema<T, S, F>(this.validator, schema, this.rootSchema, rawFormData);
}

/** Sanitize the `data` associated with the `oldSchema` so it is considered appropriate for the `newSchema`. If the
Expand All @@ -303,13 +249,7 @@ class SchemaUtils<
* to `undefined`. Will return `undefined` if the new schema is not an object containing properties.
*/
sanitizeDataForNewSchema(newSchema?: S, oldSchema?: S, data?: any): T {
return sanitizeDataForNewSchema(
this.validator,
this.rootSchema,
newSchema,
oldSchema,
data,
);
return sanitizeDataForNewSchema(this.validator, this.rootSchema, newSchema, oldSchema, data);
}

/** Generates an `IdSchema` object for the `schema`, recursively
Expand All @@ -321,22 +261,8 @@ class SchemaUtils<
* @param [idSeparator='_'] - The separator to use for the path segments in the id
* @returns - The `IdSchema` object for the `schema`
*/
toIdSchema(
schema: S,
id?: string | null,
formData?: T,
idPrefix = "root",
idSeparator = "_",
): IdSchema<T> {
return toIdSchema<T, S, F>(
this.validator,
schema,
id,
this.rootSchema,
formData,
idPrefix,
idSeparator,
);
toIdSchema(schema: S, id?: string | null, formData?: T, idPrefix = 'root', idSeparator = '_'): IdSchema<T> {
return toIdSchema<T, S, F>(this.validator, schema, id, this.rootSchema, formData, idPrefix, idSeparator);
}

/** Generates an `PathSchema` object for the `schema`, recursively
Expand All @@ -347,13 +273,7 @@ class SchemaUtils<
* @returns - The `PathSchema` object for the `schema`
*/
toPathSchema(schema: S, name?: string, formData?: T): PathSchema<T> {
return toPathSchema<T, S, F>(
this.validator,
schema,
name,
this.rootSchema,
formData,
);
return toPathSchema<T, S, F>(this.validator, schema, name, this.rootSchema, formData);
}
}

Expand All @@ -368,15 +288,11 @@ class SchemaUtils<
export default function createSchemaUtils<
T = any,
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any,
F extends FormContextType = any
>(
validator: ValidatorType<T, S, F>,
rootSchema: S,
experimental_defaultFormStateBehavior = {},
experimental_defaultFormStateBehavior = {}
): SchemaUtilsType<T, S, F> {
return new SchemaUtils<T, S, F>(
validator,
rootSchema,
experimental_defaultFormStateBehavior,
);
return new SchemaUtils<T, S, F>(validator, rootSchema, experimental_defaultFormStateBehavior);
}
16 changes: 7 additions & 9 deletions packages/utils/src/enumOptionsDeselectValue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EnumOptionsType, RJSFSchema, StrictRJSFSchema } from "./types";
import enumOptionsValueForIndex from "./enumOptionsValueForIndex";
import deepEquals from "./deepEquals";
import { EnumOptionsType, RJSFSchema, StrictRJSFSchema } from './types';
import enumOptionsValueForIndex from './enumOptionsValueForIndex';
import deepEquals from './deepEquals';

/** Removes the enum option value at the `valueIndex` from the currently `selected` (list of) value(s). If `selected` is
* a list, then that list is updated to remove the enum option value with the `valueIndex` in `allEnumOptions`. If it is
Expand All @@ -14,13 +14,11 @@ import deepEquals from "./deepEquals";
* unless `selected` is a single value. In that case, if the `valueIndex` value matches `selected`, returns
* undefined, otherwise `selected`.
*/
export default function enumOptionsDeselectValue<
S extends StrictRJSFSchema = RJSFSchema,
>(
export default function enumOptionsDeselectValue<S extends StrictRJSFSchema = RJSFSchema>(
valueIndex: string | number,
selected?: EnumOptionsType<S>["value"] | EnumOptionsType<S>["value"][],
allEnumOptions: EnumOptionsType<S>[] = [],
): EnumOptionsType<S>["value"] | EnumOptionsType<S>["value"][] | undefined {
selected?: EnumOptionsType<S>['value'] | EnumOptionsType<S>['value'][],
allEnumOptions: EnumOptionsType<S>[] = []
): EnumOptionsType<S>['value'] | EnumOptionsType<S>['value'][] | undefined {
const value = enumOptionsValueForIndex<S>(valueIndex, allEnumOptions);
if (Array.isArray(selected)) {
return selected.filter((v) => !deepEquals(v, value));
Expand Down
12 changes: 5 additions & 7 deletions packages/utils/src/enumOptionsIsSelected.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import deepEquals from "./deepEquals";
import { EnumOptionsType, RJSFSchema, StrictRJSFSchema } from "./types";
import deepEquals from './deepEquals';
import { EnumOptionsType, RJSFSchema, StrictRJSFSchema } from './types';

/** Determines whether the given `value` is (one of) the `selected` value(s).
*
* @param value - The value being checked to see if it is selected
* @param selected - The current selected value or list of values
* @returns - true if the `value` is one of the `selected` ones, false otherwise
*/
export default function enumOptionsIsSelected<
S extends StrictRJSFSchema = RJSFSchema,
>(
value: EnumOptionsType<S>["value"],
selected: EnumOptionsType<S>["value"] | EnumOptionsType<S>["value"][],
export default function enumOptionsIsSelected<S extends StrictRJSFSchema = RJSFSchema>(
value: EnumOptionsType<S>['value'],
selected: EnumOptionsType<S>['value'] | EnumOptionsType<S>['value'][]
) {
if (Array.isArray(selected)) {
return selected.some((sel) => deepEquals(sel, value));
Expand Down
Loading

0 comments on commit faf212f

Please sign in to comment.