Skip to content

Commit

Permalink
feat(validator): support array
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm authored and AliMD committed Mar 7, 2023
1 parent 88afae3 commit b632a7a
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions core/validator/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type {JsonSchema};

export function validator<T extends StringifyableRecord>(
validSchema: JsonSchema,
targetObject: StringifyableRecord,
targetObject?: StringifyableRecord | Array<StringifyableRecord> | null,
additionalProperties = false,
path = '.',
): T {
Expand Down Expand Up @@ -43,7 +43,20 @@ export function validator<T extends StringifyableRecord>(
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<StringifyableRecord>(
item,
itemValue[index] as Array<StringifyableRecord>,
additionalProperties,
itemPath[index],
);
}
}
else if (typeof itemSchema === 'object' && itemSchema != null) {
// nested object
targetObject[itemName] = validator<StringifyableRecord>(
itemSchema,
Expand Down

0 comments on commit b632a7a

Please sign in to comment.