Skip to content

Commit

Permalink
feat(property): add validator for property
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 23, 2023
1 parent 9ce9e84 commit 9e46ffd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions combinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { TypeValidator } from "./validators/type.ts";
import { InstanceValidator } from "./validators/instance.ts";
import { PropertyValidator } from "./validators/property.ts";
import { DictionaryValidator } from "./validators/object/dictionary.ts";
import { OptionalValidator } from "./validators/object/optional.ts";
import { NullishValidator } from "./validators/nullish.ts";
Expand Down Expand Up @@ -36,6 +37,9 @@ export const lt = /* @__PURE__ */ lazy(LessValidator);
export const gt = /* @__PURE__ */ lazy(GreaterValidator);
export const and = /* @__PURE__ */ lazy(AndValidator);

// known
export const property = /* @__PURE__ */ lazy(PropertyValidator);

// Date
export const validDate = /* @__PURE__ */ new ValidDateValidator();

Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export {
optional,
pattern,
positive,
property,
string,
type,
unique,
Expand Down
35 changes: 35 additions & 0 deletions validators/property.ts
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}`;
}
}

0 comments on commit 9e46ffd

Please sign in to comment.