Skip to content

Commit

Permalink
Fix IdSchema and PathSchema types
Browse files Browse the repository at this point in the history
  • Loading branch information
solimant committed Jun 22, 2024
1 parent 7bcfc4a commit bf97053
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 43 deletions.
6 changes: 5 additions & 1 deletion packages/utils/src/schema/toIdSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ function toIdSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema, F
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 { [key in keyof IdSchema<T>]: IdSchema<T> })[name as keyof IdSchema<T>] = toIdSchemaInternal<
T,
S,
F
>(
validator,
isObject(field) ? field : {},
idPrefix,
Expand Down
65 changes: 31 additions & 34 deletions packages/utils/src/schema/toPathSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,52 +78,49 @@ 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>(
validator,
schemaItems[i] as S,
`${name}.${i}`,
rootSchema,
element,
_recurseList
);
(pathSchema as { [key in keyof PathSchema<T>]: PathSchema<T> })[i as keyof PathSchema<T>] =
toPathSchemaInternal<T, S, F>(
validator,
schemaItems[i] as S,
`${name}.${i}`,
rootSchema,
element,
_recurseList
);
} else if (schemaAdditionalItems) {
pathSchema[i] = toPathSchemaInternal<T, S, F>(
validator,
schemaAdditionalItems as S,
`${name}.${i}`,
rootSchema,
element,
_recurseList
);
(pathSchema as { [key in keyof PathSchema<T>]: PathSchema<T> })[i as keyof PathSchema<T>] =
toPathSchemaInternal<T, S, F>(
validator,
schemaAdditionalItems as S,
`${name}.${i}`,
rootSchema,
element,
_recurseList
);
} else {
console.warn(`Unable to generate path schema for "${name}.${i}". No schema defined for it`);
}
});
} else {
formData.forEach((element, i: number) => {
pathSchema[i] = toPathSchemaInternal<T, S, F>(
validator,
schemaItems as S,
`${name}.${i}`,
rootSchema,
element,
_recurseList
);
(pathSchema as { [key in keyof PathSchema<T>]: PathSchema<T> })[i as keyof PathSchema<T>] =
toPathSchemaInternal<T, S, F>(validator, schemaItems as S, `${name}.${i}`, rootSchema, element, _recurseList);
});
}
} 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>(
validator,
field,
`${name}.${property}`,
rootSchema,
// It's possible that formData is not an object -- this can happen if an
// array item has just been added, but not populated with data yet
get(formData, [property]),
_recurseList
);
(pathSchema as { [key in keyof PathSchema<T>]: PathSchema<T> })[property as keyof PathSchema<T>] =
toPathSchemaInternal<T, S, F>(
validator,
field,
`${name}.${property}`,
rootSchema,
// It's possible that formData is not an object -- this can happen if an
// array item has just been added, but not populated with data yet
get(formData, [property]),
_recurseList
);
}
}
return pathSchema as PathSchema<T>;
Expand Down
20 changes: 12 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,12 @@ 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 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

0 comments on commit bf97053

Please sign in to comment.