Skip to content

Commit

Permalink
feat(property_key): rename validator to property key from key
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jun 2, 2023
1 parent 35b0bec commit db65b8e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
shouldBeBut,
} from "./utils.ts";
import { EnumValidator } from "./validators/enum.ts";
import { KeyValidator } from "./validators/key.ts";
import { RangeValidator } from "./validators/range.ts";
import { FixedArrayValidator } from "./validators/array/fixed_array.ts";
import { PropertiesValidator } from "./validators/object/properties.ts";
import { OptionalValidator } from "./validators/object/optional.ts";
import { PropertyValueValidator } from "./validators/object/property_value.ts";
import { PropertyKeyValidator } from "./validators/object/property_key.ts";
import { NullishValidator } from "./validators/nullish.ts";
import { FloatValidator } from "./validators/number/float.ts";
import { IntegerValidator } from "./validators/number/integer.ts";
Expand Down Expand Up @@ -478,7 +478,7 @@ export function between<In>(min: In, max: In) {
* const keyValidator = key(validator);
* ```
*/
export const key = /* @__PURE__ */ createInst(KeyValidator);
export const key = /* @__PURE__ */ createInst(PropertyKeyValidator);

/** Factory for property value validator.
*
Expand Down
2 changes: 1 addition & 1 deletion docs/binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Objects implementing this interface also come with default messages.
| [optional](#optional) | Closure | OptionalValidator | |
| [enumerator](#enumerator) | Closure | EnumValidator ||
| [nullish](#nullish) | Object | NullishValidator ||
| [key](#key) | Closure | KeyValidator | |
| [key](#key) | Closure | PropertyKeyValidator | |
| [value](#value) | Closure | PropertyValueValidator | |
| [and](#and) | Closure | AndValidator ||
| [or](#or) | Closure | OrValidator ||
Expand Down
2 changes: 1 addition & 1 deletion docs/validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ do narrowing, but check the implementation.
| Name | In |
| --------------------------- | --------------------------------------- |
| EnumValidator | `unknown` |
| KeyValidator | `{}` |
| NullishValidator | `unknown` |
| RangeValidator | `unknown` |
| PatternValidator | `string` |
Expand All @@ -218,6 +217,7 @@ do narrowing, but check the implementation.
| PropertiesValidator | `Record<PropertyKey, unknown>` |
| OptionalValidator | `Partial<Record<PropertyKey, unknown>>` |
| PropertyValueValidator | `Record<string, unknown>` |
| PropertyKeyValidator | `object` |
| NegativeNumberValidator | `number` &#124; `bigint` |
| NonNegativeNumberValidator | `number` &#124; `bigint` |
| NonPositiveNumberValidator | `number` &#124; `bigint` |
Expand Down
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export {
ScalarValidator,
} from "./validators/utils.ts";
export { EnumValidator } from "./validators/enum.ts";
export { KeyValidator } from "./validators/key.ts";
export { NullishValidator } from "./validators/nullish.ts";
export { RangeValidator } from "./validators/range.ts";
export { FixedArrayValidator } from "./validators/array/fixed_array.ts";
Expand All @@ -99,6 +98,7 @@ export { PositiveNumberValidator } from "./validators/numeric/positive_number.ts
export { PropertiesValidator } from "./validators/object/properties.ts";
export { OptionalValidator } from "./validators/object/optional.ts";
export { PropertyValueValidator } from "./validators/object/property_value.ts";
export { PropertyKeyValidator } from "./validators/object/property_key.ts";
export { AndValidator } from "./validators/operators/and.ts";
export { EqualityValidator } from "./validators/operators/equality.ts";
export { GreaterThanValidator } from "./validators/operators/greater_than.ts";
Expand Down
20 changes: 10 additions & 10 deletions validators/key.ts → validators/object/property_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

import { BasicValidator } from "./utils.ts";
import { curryR } from "../deps.ts";
import { fromPath, print } from "../utils.ts";
import { map } from "../iter_utils.ts";
import { type ValidationFailure, type Validator } from "../types.ts";
import { BasicValidator } from "../utils.ts";
import { curryR } from "../../deps.ts";
import { fromPath, print } from "../../utils.ts";
import { map } from "../../iter_utils.ts";
import { type ValidationFailure, type Validator } from "../../types.ts";

/** Property key validator. It checks to pass all property key.
*
* @example
* ```ts
* import { KeyValidator } from "https://deno.land/x/abstruct@$VERSION/validators/key.ts";
* import { PropertyKeyValidator } from "https://deno.land/x/abstruct@$VERSION/validators/object/property_key.ts";
* import { type Validator } from "https://deno.land/x/abstruct@$VERSION/types.ts";
* declare const validator: Validator<string>;
* const keyValidator = new KeyValidator(validator);
* const keyValidator = new PropertyKeyValidator(validator);
* ```
*/
export class KeyValidator<T extends string = string>
extends BasicValidator<{}, Record<T, unknown>> {
export class PropertyKeyValidator<T extends string = string>
extends BasicValidator<object, Record<T, unknown>> {
validator: Validator<string, T>;

constructor(validator: Readonly<Validator<string, T>>) {
super();
this.validator = validator;
}

*validate(input: {}): Iterable<ValidationFailure> {
*validate(input: object): Iterable<ValidationFailure> {
for (const key in input) {
const createError = curryR(fromPath, key);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.

import { KeyValidator } from "./key.ts";
import { ValidationFailure } from "../types.ts";
import { PatternValidator } from "./string/pattern.ts";
import { assertEquals, describe, it } from "../_dev_deps.ts";
import { PropertyKeyValidator } from "./property_key.ts";
import { ValidationFailure } from "../../types.ts";
import { PatternValidator } from "../string/pattern.ts";
import { assertEquals, describe, it } from "../../_dev_deps.ts";

describe("KeyValidator", () => {
describe("PropertyKeyValidator", () => {
it("validate should yield failures", () => {
const validator = new KeyValidator(new PatternValidator(/^a/));
const validator = new PropertyKeyValidator(new PatternValidator(/^a/));

assertEquals([...validator.validate({ b: "", bb: "" })], [
new ValidationFailure("", { instancePath: ["b"] }),
Expand All @@ -16,7 +16,7 @@ describe("KeyValidator", () => {
});

it("should represent of", () => {
const validator = new KeyValidator(new PatternValidator(/^a/));
const validator = new PropertyKeyValidator(new PatternValidator(/^a/));

assertEquals(validator.toString(), "key of pattern of \`\/^a\/\`");
});
Expand Down

0 comments on commit db65b8e

Please sign in to comment.