From 1b9057f4a6767e9d6f5e3c232a77c40ec21fc1d8 Mon Sep 17 00:00:00 2001 From: solimant Date: Sun, 19 May 2024 02:27:33 -0400 Subject: [PATCH] Fix IdSchema and PathSchema types --- packages/utils/src/schema/toIdSchema.ts | 6 +- packages/utils/src/schema/toPathSchema.ts | 65 +++++++++---------- packages/utils/src/types.ts | 20 +++--- .../test/utilsTests/getTestValidator.ts | 2 +- .../test/utilsTests/getTestValidator.ts | 2 +- 5 files changed, 50 insertions(+), 45 deletions(-) diff --git a/packages/utils/src/schema/toIdSchema.ts b/packages/utils/src/schema/toIdSchema.ts index 831404fbda..d372aaa59a 100644 --- a/packages/utils/src/schema/toIdSchema.ts +++ b/packages/utils/src/schema/toIdSchema.ts @@ -64,7 +64,11 @@ function toIdSchemaInternal( + (idSchema as { [key in keyof IdSchema]: IdSchema })[name as keyof IdSchema] = toIdSchemaInternal< + T, + S, + F + >( validator, isObject(field) ? field : {}, idPrefix, diff --git a/packages/utils/src/schema/toPathSchema.ts b/packages/utils/src/schema/toPathSchema.ts index ea168361bc..f0f4795428 100644 --- a/packages/utils/src/schema/toPathSchema.ts +++ b/packages/utils/src/schema/toPathSchema.ts @@ -78,52 +78,49 @@ function toPathSchemaInternal { if (schemaItems[i]) { - pathSchema[i] = toPathSchemaInternal( - validator, - schemaItems[i] as S, - `${name}.${i}`, - rootSchema, - element, - _recurseList - ); + (pathSchema as { [key in keyof PathSchema]: PathSchema })[i as keyof PathSchema] = + toPathSchemaInternal( + validator, + schemaItems[i] as S, + `${name}.${i}`, + rootSchema, + element, + _recurseList + ); } else if (schemaAdditionalItems) { - pathSchema[i] = toPathSchemaInternal( - validator, - schemaAdditionalItems as S, - `${name}.${i}`, - rootSchema, - element, - _recurseList - ); + (pathSchema as { [key in keyof PathSchema]: PathSchema })[i as keyof PathSchema] = + toPathSchemaInternal( + 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( - validator, - schemaItems as S, - `${name}.${i}`, - rootSchema, - element, - _recurseList - ); + (pathSchema as { [key in keyof PathSchema]: PathSchema })[i as keyof PathSchema] = + toPathSchemaInternal(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( - 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]: PathSchema })[property as keyof PathSchema] = + toPathSchemaInternal( + 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; diff --git a/packages/utils/src/types.ts b/packages/utils/src/types.ts index 4343be2353..2f031afd24 100644 --- a/packages/utils/src/types.ts +++ b/packages/utils/src/types.ts @@ -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 = FieldId & { - /** The set of ids for fields in the recursive object structure */ - [key in keyof T]?: IdSchema; -}; +export type IdSchema = T extends GenericObjectType + ? FieldId & { + /** The set of ids for fields in the recursive object structure */ + [key in keyof T]?: IdSchema; + } + : FieldId; /** Type describing a name used for a field in the `PathSchema` */ export type FieldPath = { @@ -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 = FieldPath & { - /** The set of names for fields in the recursive object structure */ - [key in keyof T]?: PathSchema; -}; +export type PathSchema = T extends GenericObjectType + ? FieldPath & { + /** The set of names for fields in the recursive object structure */ + [key in keyof T]?: PathSchema; + } + : FieldPath; /** The type for error produced by RJSF schema validation */ export type RJSFValidationError = { diff --git a/packages/validator-ajv6/test/utilsTests/getTestValidator.ts b/packages/validator-ajv6/test/utilsTests/getTestValidator.ts index 9119b6f9b1..86a1466e15 100644 --- a/packages/validator-ajv6/test/utilsTests/getTestValidator.ts +++ b/packages/validator-ajv6/test/utilsTests/getTestValidator.ts @@ -22,7 +22,7 @@ export default function getTestValidator(options: CustomValidatorOption validateFormData( formData: T, schema: RJSFSchema, - customValidate?: CustomValidator, + customValidate?: CustomValidator, transformErrors?: ErrorTransformer ): ValidationData { return validator.validateFormData(formData, schema, customValidate, transformErrors); diff --git a/packages/validator-ajv8/test/utilsTests/getTestValidator.ts b/packages/validator-ajv8/test/utilsTests/getTestValidator.ts index 59f5e25ac2..5bf8c834e4 100644 --- a/packages/validator-ajv8/test/utilsTests/getTestValidator.ts +++ b/packages/validator-ajv8/test/utilsTests/getTestValidator.ts @@ -22,7 +22,7 @@ export default function getTestValidator(options: CustomValidatorOption validateFormData( formData: T | undefined, schema: RJSFSchema, - customValidate?: CustomValidator, + customValidate?: CustomValidator, transformErrors?: ErrorTransformer ): ValidationData { return validator.validateFormData(formData, schema, customValidate, transformErrors);