From b632a7a6b752fcd8666e6d42da64ebf3b0917c8d Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Tue, 7 Mar 2023 11:47:10 +0330 Subject: [PATCH] feat(validator): support array --- core/validator/src/validator.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/core/validator/src/validator.ts b/core/validator/src/validator.ts index fe97829f3..ef3f4bf74 100644 --- a/core/validator/src/validator.ts +++ b/core/validator/src/validator.ts @@ -7,7 +7,7 @@ export type {JsonSchema}; export function validator( validSchema: JsonSchema, - targetObject: StringifyableRecord, + targetObject?: StringifyableRecord | Array | null, additionalProperties = false, path = '.', ): T { @@ -43,7 +43,20 @@ export function validator( const itemSchema = validSchema[itemName]; const itemValue = targetObject[itemName] as Stringifyable; - if (typeof itemSchema === 'object' && itemSchema != null) { + if (Array.isArray(itemSchema)) { + // array + for (const index in itemSchema) { + if (!Object.prototype.hasOwnProperty.call(itemSchema, index)) continue; + const item = itemSchema[index]; + targetObject[index] = validator( + item, + itemValue[index] as Array, + additionalProperties, + itemPath[index], + ); + } + } + else if (typeof itemSchema === 'object' && itemSchema != null) { // nested object targetObject[itemName] = validator( itemSchema,