Skip to content

Commit 1b9057f

Browse files
committed
Fix IdSchema and PathSchema types
1 parent 7bcfc4a commit 1b9057f

File tree

5 files changed

+50
-45
lines changed

5 files changed

+50
-45
lines changed

packages/utils/src/schema/toIdSchema.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ function toIdSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema, F
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 { [key in keyof IdSchema<T>]: IdSchema<T> })[name as keyof IdSchema<T>] = toIdSchemaInternal<
68+
T,
69+
S,
70+
F
71+
>(
6872
validator,
6973
isObject(field) ? field : {},
7074
idPrefix,

packages/utils/src/schema/toPathSchema.ts

+31-34
Original file line numberDiff line numberDiff line change
@@ -78,52 +78,49 @@ 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>(
82-
validator,
83-
schemaItems[i] as S,
84-
`${name}.${i}`,
85-
rootSchema,
86-
element,
87-
_recurseList
88-
);
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+
);
8990
} else if (schemaAdditionalItems) {
90-
pathSchema[i] = toPathSchemaInternal<T, S, F>(
91-
validator,
92-
schemaAdditionalItems as S,
93-
`${name}.${i}`,
94-
rootSchema,
95-
element,
96-
_recurseList
97-
);
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+
);
98100
} else {
99101
console.warn(`Unable to generate path schema for "${name}.${i}". No schema defined for it`);
100102
}
101103
});
102104
} else {
103105
formData.forEach((element, i: number) => {
104-
pathSchema[i] = toPathSchemaInternal<T, S, F>(
105-
validator,
106-
schemaItems as S,
107-
`${name}.${i}`,
108-
rootSchema,
109-
element,
110-
_recurseList
111-
);
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);
112108
});
113109
}
114110
} else if (PROPERTIES_KEY in schema) {
115111
for (const property in schema.properties) {
116112
const field = get(schema, [PROPERTIES_KEY, property]);
117-
pathSchema[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-
);
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+
);
127124
}
128125
}
129126
return pathSchema as PathSchema<T>;

packages/utils/src/types.ts

+12-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,12 @@ 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 GenericObjectType
149+
? FieldPath & {
150+
/** The set of names for fields in the recursive object structure */
151+
[key in keyof T]?: PathSchema<T[key]>;
152+
}
153+
: FieldPath;
150154

151155
/** The type for error produced by RJSF schema validation */
152156
export type RJSFValidationError = {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function getTestValidator<T = any>(options: CustomValidatorOption
2222
validateFormData(
2323
formData: T,
2424
schema: RJSFSchema,
25-
customValidate?: CustomValidator<T>,
25+
customValidate?: CustomValidator,
2626
transformErrors?: ErrorTransformer
2727
): ValidationData<T> {
2828
return validator.validateFormData(formData, schema, customValidate, transformErrors);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function getTestValidator<T = any>(options: CustomValidatorOption
2222
validateFormData(
2323
formData: T | undefined,
2424
schema: RJSFSchema,
25-
customValidate?: CustomValidator<T>,
25+
customValidate?: CustomValidator,
2626
transformErrors?: ErrorTransformer
2727
): ValidationData<T> {
2828
return validator.validateFormData(formData, schema, customValidate, transformErrors);

0 commit comments

Comments
 (0)