Skip to content

Commit 3eb7e61

Browse files
committed
Simplify type assertions
1 parent a432321 commit 3eb7e61

File tree

7 files changed

+68
-51
lines changed

7 files changed

+68
-51
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
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

Lines changed: 4 additions & 8 deletions
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,16 +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 as { [key in keyof IdSchema<T>]: IdSchema<T> })[name as keyof IdSchema<T>] = toIdSchemaInternal<
68-
T,
69-
S,
70-
F
71-
>(
67+
(idSchema as IdSchema<GenericObjectType>)[name] = toIdSchemaInternal<T, S, F>(
7268
validator,
7369
isObject(field) ? field : {},
7470
idPrefix,
@@ -82,7 +78,7 @@ function toIdSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema, F
8278
);
8379
}
8480
}
85-
return idSchema as IdSchema<T>;
81+
return idSchema;
8682
}
8783

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

packages/utils/src/schema/toPathSchema.ts

Lines changed: 38 additions & 35 deletions
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,52 +78,55 @@ 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 as { [key in keyof PathSchema<T>]: PathSchema<T> })[i as keyof PathSchema<T>] =
82-
toPathSchemaInternal<T, S, F>(
83-
validator,
84-
schemaItems[i] as S,
85-
`${name}.${i}`,
86-
rootSchema,
87-
element,
88-
_recurseList
89-
);
81+
(pathSchema as PathSchema<T[]>)[i] = toPathSchemaInternal<T, S, F>(
82+
validator,
83+
schemaItems[i] as S,
84+
`${name}.${i}`,
85+
rootSchema,
86+
element,
87+
_recurseList
88+
);
9089
} else if (schemaAdditionalItems) {
91-
(pathSchema as { [key in keyof PathSchema<T>]: PathSchema<T> })[i as keyof PathSchema<T>] =
92-
toPathSchemaInternal<T, S, F>(
93-
validator,
94-
schemaAdditionalItems as S,
95-
`${name}.${i}`,
96-
rootSchema,
97-
element,
98-
_recurseList
99-
);
90+
(pathSchema as PathSchema<T[]>)[i] = toPathSchemaInternal<T, S, F>(
91+
validator,
92+
schemaAdditionalItems as S,
93+
`${name}.${i}`,
94+
rootSchema,
95+
element,
96+
_recurseList
97+
);
10098
} else {
10199
console.warn(`Unable to generate path schema for "${name}.${i}". No schema defined for it`);
102100
}
103101
});
104102
} else {
105103
formData.forEach((element, i: number) => {
106-
(pathSchema as { [key in keyof PathSchema<T>]: PathSchema<T> })[i as keyof PathSchema<T>] =
107-
toPathSchemaInternal<T, S, F>(validator, schemaItems as S, `${name}.${i}`, rootSchema, element, _recurseList);
104+
(pathSchema as PathSchema<T[]>)[i] = toPathSchemaInternal<T, S, F>(
105+
validator,
106+
schemaItems as S,
107+
`${name}.${i}`,
108+
rootSchema,
109+
element,
110+
_recurseList
111+
);
108112
});
109113
}
110114
} else if (PROPERTIES_KEY in schema) {
111115
for (const property in schema.properties) {
112116
const field = get(schema, [PROPERTIES_KEY, property]);
113-
(pathSchema as { [key in keyof PathSchema<T>]: PathSchema<T> })[property as keyof PathSchema<T>] =
114-
toPathSchemaInternal<T, S, F>(
115-
validator,
116-
field,
117-
`${name}.${property}`,
118-
rootSchema,
119-
// It's possible that formData is not an object -- this can happen if an
120-
// array item has just been added, but not populated with data yet
121-
get(formData, [property]),
122-
_recurseList
123-
);
117+
(pathSchema as PathSchema<GenericObjectType>)[property] = toPathSchemaInternal<T, S, F>(
118+
validator,
119+
field,
120+
`${name}.${property}`,
121+
rootSchema,
122+
// It's possible that formData is not an object -- this can happen if an
123+
// array item has just been added, but not populated with data yet
124+
get(formData, [property]),
125+
_recurseList
126+
);
124127
}
125128
}
126-
return pathSchema as PathSchema<T>;
129+
return pathSchema;
127130
}
128131

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

packages/utils/src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ export type FieldPath = {
145145
};
146146

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

packages/utils/test/schema/types.ts

Lines changed: 1 addition & 1 deletion
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
1111
setReturnValues(params?: TestValidatorParams): void;
1212
}

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

Lines changed: 3 additions & 3 deletions
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,
25-
customValidate?: CustomValidator,
26-
transformErrors?: ErrorTransformer
25+
customValidate?: CustomValidator<T>,
26+
transformErrors?: ErrorTransformer<T>
2727
): ValidationData<T> {
2828
return validator.validateFormData(formData, schema, customValidate, transformErrors);
2929
},

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

Lines changed: 3 additions & 3 deletions
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,
25-
customValidate?: CustomValidator,
26-
transformErrors?: ErrorTransformer
25+
customValidate?: CustomValidator<T>,
26+
transformErrors?: ErrorTransformer<T>
2727
): ValidationData<T> {
2828
return validator.validateFormData(formData, schema, customValidate, transformErrors);
2929
},

0 commit comments

Comments
 (0)