Skip to content

Commit 2851f78

Browse files
committed
Some updates
1 parent 41fe5e8 commit 2851f78

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-spec",
3-
"version": "0.1.69",
3+
"version": "0.1.70",
44
"license": "LGPL-3.0",
55
"homepage": "https://github.com/js-works/js-spec",
66
"main": "index.js",

src/main/api/Spec.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,12 +489,12 @@ const Spec = {
489489
return specValidator || Spec.any
490490
},
491491

492-
checkProps({ required, optional, extensible = false, validate }: {
493-
required?: Record<string, Validator>,
494-
optional?: Record<string, Validator>,
495-
extensible?: boolean,
496-
validate?: Validator
497-
}): SpecValidator {
492+
checkProps<T extends object = any>({
493+
required,
494+
optional,
495+
extensible,
496+
validate
497+
}: CheckPropsConfig<T>): SpecValidator {
498498
return Spec.and(
499499
Spec.props({ required, optional, validate }),
500500

@@ -1088,3 +1088,18 @@ function _checkConstraint(constraint: Validator, it: any, path: null | string =
10881088

10891089
return ret
10901090
}
1091+
1092+
type PickOptionalProps<T extends object> = Pick<T, {
1093+
[K in keyof T]-?: T extends Record<K, T[K]> ? never : K
1094+
}[keyof T]>
1095+
1096+
type PickRequiredProps<T extends object> = Pick<T, {
1097+
[K in keyof T]-?: T extends Record<K, T[K]> ? K : never
1098+
}[keyof T]>
1099+
1100+
type CheckPropsConfig<T extends object = any> = {
1101+
required?: { [K in keyof Partial<PickRequiredProps<T>>]: Validator<T[K]> },
1102+
optional?: { [K in keyof PickOptionalProps<T>]: Validator<Exclude<T[K], undefined>> },
1103+
extensible?: boolean,
1104+
validate?: Validator<T>
1105+
}

src/main/api/Validator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type Validator =
2-
(it: any, path?: string | null) => null | boolean | Error
1+
type Validator<T = any> =
2+
(it: T, path?: string | null) => null | boolean | Error
33

44
export default Validator

0 commit comments

Comments
 (0)