diff --git a/src/ObjectType.ts b/src/ObjectType.ts index f119c88..6c2957a 100644 --- a/src/ObjectType.ts +++ b/src/ObjectType.ts @@ -52,7 +52,7 @@ export class ObjectType extends MixedType< return validator(value, type.rules) || { hasError: false }; }; - return check(value, data, this) as CheckResult; + return check(value, data, this) as CheckResult; } checkAsync(value: PlainObject = this.value, data?: DataType, fieldName?: string | string[]) { @@ -83,7 +83,7 @@ export class ObjectType extends MixedType< } return validator(value, type.priorityRules) - .then((checkStatus: CheckResult | void | null) => { + .then((checkStatus: CheckResult | void | null) => { if (checkStatus) { resolve(checkStatus); } @@ -94,7 +94,7 @@ export class ObjectType extends MixedType< } }) .then(() => validator(value, type.rules)) - .then((checkStatus: CheckResult | void | null) => { + .then((checkStatus: CheckResult | void | null) => { if (checkStatus) { resolve(checkStatus); } @@ -103,7 +103,7 @@ export class ObjectType extends MixedType< }); }; - return check(value, data, this) as Promise>; + return check(value, data, this) as Promise>; } /** diff --git a/src/types.ts b/src/types.ts index ab3f8a4..7eabb6d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,10 +7,12 @@ import { ObjectType } from './ObjectType'; export type TypeName = 'array' | 'string' | 'boolean' | 'number' | 'object' | 'date'; -export interface CheckResult { +export interface CheckResult { hasError?: boolean; errorMessage?: E | string; - object?: CheckResult; + object?: { + [P in keyof DataType]: CheckResult; + }; array?: CheckResult[]; } export type ErrorMessageType = string; diff --git a/types/test.ts b/types/test.ts index 989dc18..7ffbb7b 100644 --- a/types/test.ts +++ b/types/test.ts @@ -237,3 +237,22 @@ ObjectType().shape({ // TS2345: Argument of type '{ b: NumberType; }' is not assignable to parameter of type 'SchemaDeclaration'. // Object literal may only specify known properties, and 'b' does not exist in type 'SchemaDeclaration'. }); + +interface F10 { + a: { + b: number; + }; +} +const schemaF10 = new Schema({ + a: ObjectType().shape({ + b: NumberType() + }) +}); + +schemaF10.check({ a: { b: 1 } }); + +// $ExpectError +const checkResultF10 = schemaF10.check({ a: { b: '1' } }); + +checkResultF10.a.object?.b.errorMessage; +checkResultF10.a.object?.b.hasError;