From 3cba023703fa84ad9dfd234108082746bc36fa9e Mon Sep 17 00:00:00 2001 From: Asad Ali Date: Wed, 30 Oct 2024 11:40:42 +0500 Subject: [PATCH] refactor: add suggested refactoring --- packages/schema/src/types/object.ts | 90 ++++++++++++++++------------- 1 file changed, 51 insertions(+), 39 deletions(-) diff --git a/packages/schema/src/types/object.ts b/packages/schema/src/types/object.ts index dd4ac2b..7d1e1a0 100644 --- a/packages/schema/src/types/object.ts +++ b/packages/schema/src/types/object.ts @@ -260,30 +260,30 @@ function validateObjectBeforeMapXml( // Validate all known elements and attributes using the schema - return validateValueObject( - 'validateBeforeMapXml', - 'child elements', - 'element', - 'element', - elementsToProps, + return validateValueObject({ + validationMethod: 'validateBeforeMapXml', + propTypeName: 'child elements', + propTypePrefix: 'element', + valueTypeName: 'element', + propMapping: elementsToProps, objectSchema, - elements, + valueObject: elements, ctxt, skipAdditionalPropValidation, - mapAdditionalProps - ).concat( - validateValueObject( - 'validateBeforeMapXml', - 'attributes', - '@', - 'element', - attributesToProps, + mapAdditionalProps, + }).concat( + validateValueObject({ + validationMethod: 'validateBeforeMapXml', + propTypeName: 'attributes', + propTypePrefix: '@', + valueTypeName: 'element', + propMapping: attributesToProps, objectSchema, - attributes, + valueObject: attributes, ctxt, skipAdditionalPropValidation, - mapAdditionalProps - ) + mapAdditionalProps, + }) ); }; } @@ -376,21 +376,32 @@ function unmapObjectToXml( }; } -function validateValueObject( +function validateValueObject({ + validationMethod, + propTypeName, + propTypePrefix, + valueTypeName, + propMapping, + objectSchema, + valueObject, + ctxt, + skipAdditionalPropValidation, + mapAdditionalProps, +}: { validationMethod: | 'validateBeforeMap' | 'validateBeforeUnmap' - | 'validateBeforeMapXml', - propTypeName: string, - propTypePrefix: string, - valueTypeName: string, - propMapping: Record, - objectSchema: AnyObjectSchema, - valueObject: { [key: string]: any }, - ctxt: SchemaContextCreator, - skipAdditionalPropValidation: boolean, - mapAdditionalProps: boolean | [string, Schema] -) { + | 'validateBeforeMapXml'; + propTypeName: string; + propTypePrefix: string; + valueTypeName: string; + propMapping: Record; + objectSchema: AnyObjectSchema; + valueObject: { [key: string]: any }; + ctxt: SchemaContextCreator; + skipAdditionalPropValidation: boolean; + mapAdditionalProps: boolean | [string, Schema]; +}) { const errors: SchemaValidationError[] = []; const missingProps: Set = new Set(); const conflictingProps: Set = new Set(); @@ -408,6 +419,7 @@ function validateValueObject( } } + // Create validation errors for conflicting additional properties keys addErrorsIfAny( conflictingProps, (names) => @@ -499,7 +511,7 @@ function validateObject( skipAdditionalPropValidation: boolean, mapAdditionalProps: boolean | [string, Schema] ) { - const propsMapping = getPropMappingForObjectSchema(objectSchema); + const propMapping = getPropMappingForObjectSchema(objectSchema); return (value: unknown, ctxt: SchemaContextCreator) => { if (typeof value !== 'object' || value === null) { return ctxt.fail(); @@ -511,18 +523,18 @@ function validateObject( }' but found 'Array<${typeof value}>'.` ); } - return validateValueObject( + return validateValueObject({ validationMethod, - 'properties', - '', - 'object', - propsMapping, + propTypeName: 'properties', + propTypePrefix: '', + valueTypeName: 'object', + propMapping, objectSchema, - value as Record, + valueObject: value as Record, ctxt, skipAdditionalPropValidation, - mapAdditionalProps - ); + mapAdditionalProps, + }); }; }