-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(value): add validator for property value
- Loading branch information
1 parent
0e6ba5b
commit cd994e3
Showing
3 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,4 +56,5 @@ export { | |
uint8, | ||
unique, | ||
validDate, | ||
value, | ||
} from "./combinator.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// deno-lint-ignore-file ban-types | ||
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
import { isEmpty } from "../deps.ts"; | ||
import { curryR, fromPath } from "../utils.ts"; | ||
import { map } from "../iter_utils.ts"; | ||
import { type ValidationFailure, Validator } from "../types.ts"; | ||
|
||
export class ValueValidator<In, In_ extends In = In> | ||
implements Validator<{}, Record<string, In_>> { | ||
validator: Validator<In, In_>; | ||
|
||
constructor(validator: Validator<In, In_>) { | ||
this.validator = validator; | ||
} | ||
|
||
is(input: {}): input is Record<string, In_> { | ||
return isEmpty(this.validate(input)); | ||
} | ||
|
||
*validate(input: {}): Iterable<ValidationFailure> { | ||
for (const [key, value] of Object.entries(input)) { | ||
const createError = curryR(fromPath, key); | ||
|
||
yield* map(this.validator.validate(value as In), createError); | ||
} | ||
} | ||
|
||
toString(): string { | ||
return `property value of ${this.validator}`; | ||
} | ||
} |