Skip to content

Commit

Permalink
feat(properties): rename to properties validator from object validator
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jun 2, 2023
1 parent e3f1106 commit 9ae301d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 64 deletions.
4 changes: 2 additions & 2 deletions bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { KeyValidator } from "./validators/key.ts";
import { ValueValidator } from "./validators/value.ts";
import { RangeValidator } from "./validators/range.ts";
import { FixedArrayValidator } from "./validators/array/fixed_array.ts";
import { ObjectValidator } from "./validators/object.ts";
import { PropertiesValidator } from "./validators/object/properties.ts";
import { OptionalValidator } from "./validators/object/optional.ts";
import { NullishValidator } from "./validators/nullish.ts";
import { FloatValidator } from "./validators/number/float.ts";
Expand Down Expand Up @@ -172,7 +172,7 @@ export function instance<T extends Constructor>(of: T) {
* const User = object({ name: string, age: number });
* ```
*/
export const object = /* @__PURE__ */ createInst(ObjectValidator);
export const object = /* @__PURE__ */ createInst(PropertiesValidator);

/** Factory for optional properties validator.
*
Expand Down
4 changes: 2 additions & 2 deletions docs/binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ Objects implementing this interface also come with default messages.
| [boolean](#instance) | Object | TypeValidator ||
| [symbol](#instance) | Object | TypeValidator ||
| [instance](#instance) | Closure | InstanceValidator ||
| [object](#object) | Closure | ObjectValidator | |
| [optional](#object) | Closure | OptionalValidator | |
| [object](#object) | Closure | PropertiesValidator | |
| [optional](#optional) | Closure | OptionalValidator | |
| [enumerator](#enumerator) | Closure | EnumValidator ||
| [nullish](#nullish) | Object | NullishValidator ||
| [key](#key) | Closure | KeyValidator | |
Expand Down
74 changes: 37 additions & 37 deletions docs/validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,40 +197,40 @@ The following is a list of standard validators.
In indicates the broadest type accepted. Most validators have generics and will
do narrowing, but check the implementation.

| Name | In |
| --------------------------- | ------------------------------ |
| EnumValidator | `unknown` |
| KeyValidator | `{}` |
| NullishValidator | `unknown` |
| ObjectValidator | `Record<PropertyKey, unknown>` |
| RangeValidator | `unknown` |
| ValueValidator | `Record<string, unknown>` |
| PatternValidator | `string` |
| AndValidator | `unknown` |
| EqualityValidator | `unknown` |
| InequalityValidator | `unknown` |
| GreaterThanValidator | `unknown` |
| GreaterThanOrEqualValidator | `unknown` |
| LessThanValidator | `unknown` |
| LessThanOrEqualValidator | `unknown` |
| InstanceValidator | `unknown` |
| NotValidatorValidator | `unknown` |
| OrValidatorValidator | `unknown` |
| TypeValidator | `unknown` |
| OptionalValidator | `Record<PropertyKey, unknown>` |
| NegativeNumberValidator | `number` &#124; `bigint` |
| NonNegativeNumberValidator | `number` &#124; `bigint` |
| NonPositiveNumberValidator | `number` &#124; `bigint` |
| PositiveNumberValidator | `number` &#124; `bigint` |
| FloatValidatorValidator | `number` |
| IntegerValidator | `number` |
| EmptyValidator | `Iterable<unknown>` |
| NonEmptyValidator | `Iterable<unknown>` |
| ItemValidator | `Iterable<unknown>` |
| CountValidator | `Iterable<unknown>` |
| MaxCountValidator | `Iterable<unknown>` |
| MinCountValidator | `Iterable<unknown>` |
| SingleValidator | `Iterable<unknown>` |
| UniqueValidator | `Iterable<unknown>` |
| ValidDateValidator | `Date` |
| FixedArrayValidator | `unknown[]` |
| Name | In |
| --------------------------- | --------------------------------------- |
| EnumValidator | `unknown` |
| KeyValidator | `{}` |
| NullishValidator | `unknown` |
| RangeValidator | `unknown` |
| ValueValidator | `Record<string, unknown>` |
| PatternValidator | `string` |
| AndValidator | `unknown` |
| EqualityValidator | `unknown` |
| InequalityValidator | `unknown` |
| GreaterThanValidator | `unknown` |
| GreaterThanOrEqualValidator | `unknown` |
| LessThanValidator | `unknown` |
| LessThanOrEqualValidator | `unknown` |
| InstanceValidator | `unknown` |
| NotValidatorValidator | `unknown` |
| OrValidatorValidator | `unknown` |
| TypeValidator | `unknown` |
| PropertiesValidator | `Record<PropertyKey, unknown>` |
| OptionalValidator | `Partial<Record<PropertyKey, unknown>>` |
| NegativeNumberValidator | `number` &#124; `bigint` |
| NonNegativeNumberValidator | `number` &#124; `bigint` |
| NonPositiveNumberValidator | `number` &#124; `bigint` |
| PositiveNumberValidator | `number` &#124; `bigint` |
| FloatValidatorValidator | `number` |
| IntegerValidator | `number` |
| EmptyValidator | `Iterable<unknown>` |
| NonEmptyValidator | `Iterable<unknown>` |
| ItemValidator | `Iterable<unknown>` |
| CountValidator | `Iterable<unknown>` |
| MaxCountValidator | `Iterable<unknown>` |
| MinCountValidator | `Iterable<unknown>` |
| SingleValidator | `Iterable<unknown>` |
| UniqueValidator | `Iterable<unknown>` |
| ValidDateValidator | `Date` |
| FixedArrayValidator | `unknown[]` |
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export {
export { EnumValidator } from "./validators/enum.ts";
export { KeyValidator } from "./validators/key.ts";
export { NullishValidator } from "./validators/nullish.ts";
export { ObjectValidator } from "./validators/object.ts";
export { RangeValidator } from "./validators/range.ts";
export { ValueValidator } from "./validators/value.ts";
export { FixedArrayValidator } from "./validators/array/fixed_array.ts";
Expand All @@ -98,6 +97,7 @@ export { NegativeNumberValidator } from "./validators/numeric/negative_number.ts
export { NonNegativeNumberValidator } from "./validators/numeric/non_negative_number.ts";
export { NonPositiveNumberValidator } from "./validators/numeric/non_positive_number.ts";
export { PositiveNumberValidator } from "./validators/numeric/positive_number.ts";
export { PropertiesValidator } from "./validators/object/properties.ts";
export { OptionalValidator } from "./validators/object/optional.ts";
export { AndValidator } from "./validators/operators/and.ts";
export { EqualityValidator } from "./validators/operators/equality.ts";
Expand Down
8 changes: 4 additions & 4 deletions validators/object/optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import { BasicValidator } from "../utils.ts";
import { filterKeys } from "../../deps.ts";
import { printProps } from "../../utils.ts";
import { ObjectValidator } from "../object.ts";
import { PropertiesValidator } from "./properties.ts";
import { ValidationFailure, Validator } from "../../types.ts";

/** Optional object validator. It checks properties, but also passes if the property does not exist.
*
* @example
* ```ts
* import { ObjectValidator } from "https://deno.land/x/abstruct@$VERSION/validators/object.ts";
* import { OptionalValidator } from "https://deno.land/x/abstruct@$VERSION/validators/object/optional.ts";
* import { type Validator } from "https://deno.land/x/abstruct@$VERSION/types.ts";
* declare const validator: Validator<string>;
* const objectValidator = new ObjectValidator({ key: validator, [Symbol()]: validator });
* const optionalValidator = new OptionalValidator({ key: validator, [Symbol()]: validator });
* ```
*/
export class OptionalValidator<
Expand All @@ -39,7 +39,7 @@ export class OptionalValidator<
Reflect.has.bind(this, input),
) as Record<string, Validator>;

yield* new ObjectValidator(validators).validate(input as T);
yield* new PropertiesValidator(validators).validate(input as T);
}

override toString(): string {
Expand Down
18 changes: 9 additions & 9 deletions validators/object.ts → validators/object/properties.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// 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 { map } from "../iter_utils.ts";
import { entriesAll, fromPath, printProps } from "../utils.ts";
import { ValidationFailure, type Validator } from "../types.ts";
import { BasicValidator } from "../utils.ts";
import { curryR } from "../../deps.ts";
import { map } from "../../iter_utils.ts";
import { entriesAll, fromPath, printProps } from "../../utils.ts";
import { ValidationFailure, type Validator } from "../../types.ts";

/** Object validator.
/** Object properties validator.
*
* @example
* ```ts
* import { ObjectValidator } from "https://deno.land/x/abstruct@$VERSION/validators/object.ts";
* import { PropertiesValidator } from "https://deno.land/x/abstruct@$VERSION/validators/object/properties.ts";
* import { type Validator } from "https://deno.land/x/abstruct@$VERSION/types.ts";
* declare const validator: Validator<string>;
* const objectValidator = new ObjectValidator({ key: validator, [Symbol()]: validator });
* const propertiesValidator = new PropertiesValidator({ key: validator, [Symbol()]: validator });
* ```
*/
export class ObjectValidator<
export class PropertiesValidator<
const In extends Record<PropertyKey, unknown>,
const RIn extends In = In,
> extends BasicValidator<In, RIn> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.

import { ObjectValidator } from "./object.ts";
import { TypeValidator } from "./operators/typeof.ts";
import { ValidationFailure } from "../types.ts";
import { assertEquals, describe, it } from "../_dev_deps.ts";
import { PropertiesValidator } from "./properties.ts";
import { TypeValidator } from "../operators/typeof.ts";
import { ValidationFailure } from "../../types.ts";
import { assertEquals, describe, it } from "../../_dev_deps.ts";

describe("ObjectValidator", () => {
describe("PropertiesValidator", () => {
it("validate should yield failures", () => {
assertEquals(
[...new ObjectValidator({ a: new TypeValidator("string") }).validate({
[...new PropertiesValidator({ a: new TypeValidator("string") }).validate({
a: 0,
})],
[new ValidationFailure("", { instancePath: ["a"] })],
);

assertEquals(
[...new ObjectValidator({
[...new PropertiesValidator({
a: new TypeValidator("string"),
[Symbol.iterator]: new TypeValidator("function"),
}).validate({
Expand All @@ -33,11 +33,11 @@ describe("ObjectValidator", () => {

it("should represent of", () => {
assertEquals(
new ObjectValidator({ a: new TypeValidator("string") }).toString(),
new PropertiesValidator({ a: new TypeValidator("string") }).toString(),
"{a: string}",
);
assertEquals(
new ObjectValidator({
new PropertiesValidator({
a: new TypeValidator("string"),
[Symbol.iterator]: new TypeValidator("number"),
}).toString(),
Expand Down

0 comments on commit 9ae301d

Please sign in to comment.