Skip to content

Commit

Permalink
fix(ObjectType): specifies type of property object in the `ObjectTy…
Browse files Browse the repository at this point in the history
…pe` check result (#46)
  • Loading branch information
simonguo authored Jun 30, 2022
1 parent 893a165 commit 0571e09
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/ObjectType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class ObjectType<DataType = any, E = ErrorMessageType> extends MixedType<
return validator(value, type.rules) || { hasError: false };
};

return check(value, data, this) as CheckResult<E | string>;
return check(value, data, this) as CheckResult<E | string, DataType>;
}

checkAsync(value: PlainObject = this.value, data?: DataType, fieldName?: string | string[]) {
Expand Down Expand Up @@ -83,7 +83,7 @@ export class ObjectType<DataType = any, E = ErrorMessageType> extends MixedType<
}

return validator(value, type.priorityRules)
.then((checkStatus: CheckResult<E | string> | void | null) => {
.then((checkStatus: CheckResult<E | string, DataType> | void | null) => {
if (checkStatus) {
resolve(checkStatus);
}
Expand All @@ -94,7 +94,7 @@ export class ObjectType<DataType = any, E = ErrorMessageType> extends MixedType<
}
})
.then(() => validator(value, type.rules))
.then((checkStatus: CheckResult<E | string> | void | null) => {
.then((checkStatus: CheckResult<E | string, DataType> | void | null) => {
if (checkStatus) {
resolve(checkStatus);
}
Expand All @@ -103,7 +103,7 @@ export class ObjectType<DataType = any, E = ErrorMessageType> extends MixedType<
});
};

return check(value, data, this) as Promise<CheckResult<E | string>>;
return check(value, data, this) as Promise<CheckResult<E | string, DataType>>;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { ObjectType } from './ObjectType';

export type TypeName = 'array' | 'string' | 'boolean' | 'number' | 'object' | 'date';

export interface CheckResult<E = string> {
export interface CheckResult<E = string, DataType = PlainObject> {
hasError?: boolean;
errorMessage?: E | string;
object?: CheckResult<E>;
object?: {
[P in keyof DataType]: CheckResult<E>;
};
array?: CheckResult<E>[];
}
export type ErrorMessageType = string;
Expand Down
19 changes: 19 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,22 @@ ObjectType<F9>().shape({
// TS2345: Argument of type '{ b: NumberType<any>; }' is not assignable to parameter of type 'SchemaDeclaration<F9>'.
// Object literal may only specify known properties, and 'b' does not exist in type 'SchemaDeclaration<F9>'.
});

interface F10 {
a: {
b: number;
};
}
const schemaF10 = new Schema<F10>({
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;

0 comments on commit 0571e09

Please sign in to comment.