Skip to content

Commit d08cf99

Browse files
Fix IdSchema and PathSchema types (#4196)
* Fix IdSchema and PathSchema types * Simplify type assertions --------- Co-authored-by: Heath C <[email protected]>
1 parent b1cf91d commit d08cf99

File tree

7 files changed

+48
-26
lines changed

7 files changed

+48
-26
lines changed

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ should change the heading of the (upcoming) version to include a major version b
1616
1717
-->
1818

19+
# 5.18.7
20+
21+
## @rjsf/utils
22+
23+
- Fix IdSchema and PathSchema types ([#4196](https://github.com/rjsf-team/react-jsonschema-form/pull/4196))
24+
25+
## @rjsf/validator-ajv6
26+
27+
- Fix IdSchema and PathSchema types ([#4196](https://github.com/rjsf-team/react-jsonschema-form/pull/4196))
28+
29+
## @rjsf/validator-ajv8
30+
31+
- Fix IdSchema and PathSchema types ([#4196](https://github.com/rjsf-team/react-jsonschema-form/pull/4196))
32+
1933
# 5.18.6
2034

2135
## @rjsf/antd

packages/utils/src/schema/toIdSchema.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import isEqual from 'lodash/isEqual';
33

44
import { ALL_OF_KEY, DEPENDENCIES_KEY, ID_KEY, ITEMS_KEY, PROPERTIES_KEY, REF_KEY } from '../constants';
55
import isObject from '../isObject';
6-
import { FormContextType, IdSchema, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
6+
import { FormContextType, GenericObjectType, IdSchema, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
77
import retrieveSchema from './retrieveSchema';
88
import getSchemaType from '../getSchemaType';
99

@@ -59,12 +59,12 @@ function toIdSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema, F
5959
);
6060
}
6161
const $id = id || idPrefix;
62-
const idSchema: IdSchema = { $id } as IdSchema<T>;
62+
const idSchema: IdSchema<T> = { $id } as IdSchema<T>;
6363
if (getSchemaType<S>(schema) === 'object' && PROPERTIES_KEY in schema) {
6464
for (const name in schema.properties) {
6565
const field = get(schema, [PROPERTIES_KEY, name]);
6666
const fieldId = idSchema[ID_KEY] + idSeparator + name;
67-
idSchema[name] = toIdSchemaInternal<T, S, F>(
67+
(idSchema as IdSchema<GenericObjectType>)[name] = toIdSchemaInternal<T, S, F>(
6868
validator,
6969
isObject(field) ? field : {},
7070
idPrefix,
@@ -78,7 +78,7 @@ function toIdSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema, F
7878
);
7979
}
8080
}
81-
return idSchema as IdSchema<T>;
81+
return idSchema;
8282
}
8383

8484
/** Generates an `IdSchema` object for the `schema`, recursively

packages/utils/src/schema/toPathSchema.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
RJSF_ADDITIONAL_PROPERTIES_FLAG,
1616
} from '../constants';
1717
import getDiscriminatorFieldFromSchema from '../getDiscriminatorFieldFromSchema';
18-
import { FormContextType, PathSchema, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
18+
import { FormContextType, GenericObjectType, PathSchema, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
1919
import getClosestMatchingOption from './getClosestMatchingOption';
2020
import retrieveSchema from './retrieveSchema';
2121

@@ -53,9 +53,9 @@ function toPathSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
5353
}
5454
}
5555

56-
let pathSchema: PathSchema = {
56+
let pathSchema: PathSchema<T> = {
5757
[NAME_KEY]: name.replace(/^\./, ''),
58-
} as PathSchema;
58+
} as PathSchema<T>;
5959

6060
if (ONE_OF_KEY in schema || ANY_OF_KEY in schema) {
6161
const xxxOf: S[] = ONE_OF_KEY in schema ? (schema.oneOf as S[]) : (schema.anyOf as S[]);
@@ -78,7 +78,7 @@ function toPathSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
7878
if (Array.isArray(schemaItems)) {
7979
formData.forEach((element, i: number) => {
8080
if (schemaItems[i]) {
81-
pathSchema[i] = toPathSchemaInternal<T, S, F>(
81+
(pathSchema as PathSchema<T[]>)[i] = toPathSchemaInternal<T, S, F>(
8282
validator,
8383
schemaItems[i] as S,
8484
`${name}.${i}`,
@@ -87,7 +87,7 @@ function toPathSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
8787
_recurseList
8888
);
8989
} else if (schemaAdditionalItems) {
90-
pathSchema[i] = toPathSchemaInternal<T, S, F>(
90+
(pathSchema as PathSchema<T[]>)[i] = toPathSchemaInternal<T, S, F>(
9191
validator,
9292
schemaAdditionalItems as S,
9393
`${name}.${i}`,
@@ -101,7 +101,7 @@ function toPathSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
101101
});
102102
} else {
103103
formData.forEach((element, i: number) => {
104-
pathSchema[i] = toPathSchemaInternal<T, S, F>(
104+
(pathSchema as PathSchema<T[]>)[i] = toPathSchemaInternal<T, S, F>(
105105
validator,
106106
schemaItems as S,
107107
`${name}.${i}`,
@@ -114,7 +114,7 @@ function toPathSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
114114
} else if (PROPERTIES_KEY in schema) {
115115
for (const property in schema.properties) {
116116
const field = get(schema, [PROPERTIES_KEY, property]);
117-
pathSchema[property] = toPathSchemaInternal<T, S, F>(
117+
(pathSchema as PathSchema<GenericObjectType>)[property] = toPathSchemaInternal<T, S, F>(
118118
validator,
119119
field,
120120
`${name}.${property}`,
@@ -126,7 +126,7 @@ function toPathSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema,
126126
);
127127
}
128128
}
129-
return pathSchema as PathSchema<T>;
129+
return pathSchema;
130130
}
131131

132132
/** Generates an `PathSchema` object for the `schema`, recursively

packages/utils/src/types.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,12 @@ export type FieldId = {
131131
};
132132

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

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

145147
/** Type describing a recursive structure of `FieldPath`s for an object with a non-empty set of keys */
146-
export type PathSchema<T = any> = FieldPath & {
147-
/** The set of names for fields in the recursive object structure */
148-
[key in keyof T]?: PathSchema<T[key]>;
149-
};
148+
export type PathSchema<T = any> = T extends Array<infer U>
149+
? FieldPath & {
150+
[i: number]: PathSchema<U>;
151+
}
152+
: T extends GenericObjectType
153+
? FieldPath & {
154+
/** The set of names for fields in the recursive object structure */
155+
[key in keyof T]?: PathSchema<T[key]>;
156+
}
157+
: FieldPath;
150158

151159
/** The type for error produced by RJSF schema validation */
152160
export type RJSFValidationError = {

packages/utils/test/schema/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface TestValidatorParams<T = any> {
66
errorList?: RJSFValidationError[][];
77
}
88

9-
export interface TestValidatorType extends ValidatorType {
9+
export interface TestValidatorType<T = any> extends ValidatorType<T> {
1010
// eslint-disable-next-line no-unused-vars
11-
setReturnValues(params?: TestValidatorParams): void;
11+
setReturnValues(params?: TestValidatorParams<T>): void;
1212
}

packages/validator-ajv6/test/utilsTests/getTestValidator.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import { customizeValidator, CustomValidatorOptionsType } from '../../src';
1616
*
1717
* @param options
1818
*/
19-
export default function getTestValidator<T = any>(options: CustomValidatorOptionsType): TestValidatorType {
19+
export default function getTestValidator<T = any>(options: CustomValidatorOptionsType): TestValidatorType<T> {
2020
const validator = customizeValidator<T>(options);
2121
return {
2222
validateFormData(
2323
formData: T,
2424
schema: RJSFSchema,
2525
customValidate?: CustomValidator<T>,
26-
transformErrors?: ErrorTransformer
26+
transformErrors?: ErrorTransformer<T>
2727
): ValidationData<T> {
2828
return validator.validateFormData(formData, schema, customValidate, transformErrors);
2929
},

packages/validator-ajv8/test/utilsTests/getTestValidator.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import { customizeValidator, CustomValidatorOptionsType } from '../../src';
1616
*
1717
* @param options
1818
*/
19-
export default function getTestValidator<T = any>(options: CustomValidatorOptionsType): TestValidatorType {
19+
export default function getTestValidator<T = any>(options: CustomValidatorOptionsType): TestValidatorType<T> {
2020
const validator = customizeValidator<T>(options);
2121
return {
2222
validateFormData(
2323
formData: T | undefined,
2424
schema: RJSFSchema,
2525
customValidate?: CustomValidator<T>,
26-
transformErrors?: ErrorTransformer
26+
transformErrors?: ErrorTransformer<T>
2727
): ValidationData<T> {
2828
return validator.validateFormData(formData, schema, customValidate, transformErrors);
2929
},

0 commit comments

Comments
 (0)