-
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(property): add validator for property
- Loading branch information
1 parent
9ce9e84
commit 9e46ffd
Showing
3 changed files
with
40 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 |
---|---|---|
|
@@ -23,6 +23,7 @@ export { | |
optional, | ||
pattern, | ||
positive, | ||
property, | ||
string, | ||
type, | ||
unique, | ||
|
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,35 @@ | ||
// deno-lint-ignore-file ban-types | ||
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
import { curryR, fromPath } from "../utils.ts"; | ||
import { map } from "../iter_utils.ts"; | ||
import { | ||
Assert, | ||
AssertiveValidator, | ||
Validation, | ||
ValidationError, | ||
Validator, | ||
} from "../types.ts"; | ||
|
||
export class PropertyValidator<In_ extends string = string> | ||
implements AssertiveValidator<{}, Record<In_, unknown>> { | ||
declare [Assert.symbol]: Record<In_, unknown>; | ||
validator: Validation<string>; | ||
|
||
constructor(validator: Validation<string, In_>) { | ||
this.validator = validator as Validator<string>; | ||
} | ||
|
||
*validate(input: {}): Iterable<ValidationError> { | ||
for (const key in input) { | ||
const createError = curryR(fromPath, key); | ||
|
||
yield* map(this.validator.validate(key), createError); | ||
} | ||
} | ||
|
||
toString(): string { | ||
return `key of ${this.validator}`; | ||
} | ||
} |