Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IdSchema and PathSchema types #4196

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ should change the heading of the (upcoming) version to include a major version b

-->

# 5.18.7

## @rjsf/utils

- Fix IdSchema and PathSchema types ([#4196](https://github.com/rjsf-team/react-jsonschema-form/pull/4196))

## @rjsf/validator-ajv6

- Fix IdSchema and PathSchema types ([#4196](https://github.com/rjsf-team/react-jsonschema-form/pull/4196))

## @rjsf/validator-ajv8

- Fix IdSchema and PathSchema types ([#4196](https://github.com/rjsf-team/react-jsonschema-form/pull/4196))

# 5.18.6

## @rjsf/antd
Expand Down
8 changes: 4 additions & 4 deletions packages/utils/src/schema/toIdSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import isEqual from 'lodash/isEqual';

import { ALL_OF_KEY, DEPENDENCIES_KEY, ID_KEY, ITEMS_KEY, PROPERTIES_KEY, REF_KEY } from '../constants';
import isObject from '../isObject';
import { FormContextType, IdSchema, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
import { FormContextType, GenericObjectType, IdSchema, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
import retrieveSchema from './retrieveSchema';
import getSchemaType from '../getSchemaType';

Expand Down Expand Up @@ -59,12 +59,12 @@ function toIdSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema, F
);
}
const $id = id || idPrefix;
const idSchema: IdSchema = { $id } as IdSchema<T>;
const idSchema: IdSchema<T> = { $id } as IdSchema<T>;
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[name] = toIdSchemaInternal<T, S, F>(
(idSchema as IdSchema<GenericObjectType>)[name] = toIdSchemaInternal<T, S, F>(
validator,
isObject(field) ? field : {},
idPrefix,
Expand All @@ -78,7 +78,7 @@ function toIdSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema, F
);
}
}
return idSchema as IdSchema<T>;
return idSchema;
}

/** Generates an `IdSchema` object for the `schema`, recursively
Expand Down
16 changes: 8 additions & 8 deletions packages/utils/src/schema/toPathSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
RJSF_ADDITIONAL_PROPERTIES_FLAG,
} from '../constants';
import getDiscriminatorFieldFromSchema from '../getDiscriminatorFieldFromSchema';
import { FormContextType, PathSchema, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
import { FormContextType, GenericObjectType, PathSchema, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
import getClosestMatchingOption from './getClosestMatchingOption';
import retrieveSchema from './retrieveSchema';

Expand Down Expand Up @@ -53,9 +53,9 @@ function toPathSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
}
}

let pathSchema: PathSchema = {
let pathSchema: PathSchema<T> = {
[NAME_KEY]: name.replace(/^\./, ''),
} as PathSchema;
} 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[]);
Expand All @@ -78,7 +78,7 @@ function toPathSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
if (Array.isArray(schemaItems)) {
formData.forEach((element, i: number) => {
if (schemaItems[i]) {
pathSchema[i] = toPathSchemaInternal<T, S, F>(
(pathSchema as PathSchema<T[]>)[i] = toPathSchemaInternal<T, S, F>(
validator,
schemaItems[i] as S,
`${name}.${i}`,
Expand All @@ -87,7 +87,7 @@ function toPathSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
_recurseList
);
} else if (schemaAdditionalItems) {
pathSchema[i] = toPathSchemaInternal<T, S, F>(
(pathSchema as PathSchema<T[]>)[i] = toPathSchemaInternal<T, S, F>(
validator,
schemaAdditionalItems as S,
`${name}.${i}`,
Expand All @@ -101,7 +101,7 @@ function toPathSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
});
} else {
formData.forEach((element, i: number) => {
pathSchema[i] = toPathSchemaInternal<T, S, F>(
(pathSchema as PathSchema<T[]>)[i] = toPathSchemaInternal<T, S, F>(
validator,
schemaItems as S,
`${name}.${i}`,
Expand All @@ -114,7 +114,7 @@ function toPathSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
} else if (PROPERTIES_KEY in schema) {
for (const property in schema.properties) {
const field = get(schema, [PROPERTIES_KEY, property]);
pathSchema[property] = toPathSchemaInternal<T, S, F>(
(pathSchema as PathSchema<GenericObjectType>)[property] = toPathSchemaInternal<T, S, F>(
validator,
field,
`${name}.${property}`,
Expand All @@ -126,7 +126,7 @@ function toPathSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
);
}
}
return pathSchema as PathSchema<T>;
return pathSchema;
}

/** Generates an `PathSchema` object for the `schema`, recursively
Expand Down
24 changes: 16 additions & 8 deletions packages/utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ export type FieldId = {
};

/** Type describing a recursive structure of `FieldId`s for an object with a non-empty set of keys */
export type IdSchema<T = any> = FieldId & {
/** The set of ids for fields in the recursive object structure */
[key in keyof T]?: IdSchema<T[key]>;
};
export type IdSchema<T = any> = T extends GenericObjectType
? FieldId & {
/** The set of ids for fields in the recursive object structure */
[key in keyof T]?: IdSchema<T[key]>;
}
: FieldId;

/** Type describing a name used for a field in the `PathSchema` */
export type FieldPath = {
Expand All @@ -143,10 +145,16 @@ export type FieldPath = {
};

/** Type describing a recursive structure of `FieldPath`s for an object with a non-empty set of keys */
export type PathSchema<T = any> = FieldPath & {
/** The set of names for fields in the recursive object structure */
[key in keyof T]?: PathSchema<T[key]>;
};
export type PathSchema<T = any> = T extends Array<infer U>
? FieldPath & {
[i: number]: PathSchema<U>;
}
: T extends GenericObjectType
? FieldPath & {
/** The set of names for fields in the recursive object structure */
[key in keyof T]?: PathSchema<T[key]>;
}
: FieldPath;

/** The type for error produced by RJSF schema validation */
export type RJSFValidationError = {
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/test/schema/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface TestValidatorParams<T = any> {
errorList?: RJSFValidationError[][];
}

export interface TestValidatorType extends ValidatorType {
export interface TestValidatorType<T = any> extends ValidatorType<T> {
// eslint-disable-next-line no-unused-vars
setReturnValues(params?: TestValidatorParams): void;
setReturnValues(params?: TestValidatorParams<T>): void;
}
4 changes: 2 additions & 2 deletions packages/validator-ajv6/test/utilsTests/getTestValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import { customizeValidator, CustomValidatorOptionsType } from '../../src';
*
* @param options
*/
export default function getTestValidator<T = any>(options: CustomValidatorOptionsType): TestValidatorType {
export default function getTestValidator<T = any>(options: CustomValidatorOptionsType): TestValidatorType<T> {
const validator = customizeValidator<T>(options);
return {
validateFormData(
formData: T,
schema: RJSFSchema,
customValidate?: CustomValidator<T>,
transformErrors?: ErrorTransformer
transformErrors?: ErrorTransformer<T>
): ValidationData<T> {
return validator.validateFormData(formData, schema, customValidate, transformErrors);
},
Expand Down
4 changes: 2 additions & 2 deletions packages/validator-ajv8/test/utilsTests/getTestValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import { customizeValidator, CustomValidatorOptionsType } from '../../src';
*
* @param options
*/
export default function getTestValidator<T = any>(options: CustomValidatorOptionsType): TestValidatorType {
export default function getTestValidator<T = any>(options: CustomValidatorOptionsType): TestValidatorType<T> {
const validator = customizeValidator<T>(options);
return {
validateFormData(
formData: T | undefined,
schema: RJSFSchema,
customValidate?: CustomValidator<T>,
transformErrors?: ErrorTransformer
transformErrors?: ErrorTransformer<T>
): ValidationData<T> {
return validator.validateFormData(formData, schema, customValidate, transformErrors);
},
Expand Down