Skip to content

Commit

Permalink
feat(prototype): add validator for prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 26, 2023
1 parent 0a782f7 commit e031ab5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion combinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { FixedArrayValidator } from "./validators/array/fixed_array.ts";
import { DictionaryValidator } from "./validators/object/dictionary.ts";
import { OptionalValidator } from "./validators/object/optional.ts";
import { NullishValidator } from "./validators/nullish.ts";
import { PrototypeValidator } from "./validators/prototype.ts";
import { IntegerValidator } from "./validators/number/integer.ts";
import { PositiveNumberValidator } from "./validators/number/positive_number.ts";
import { NegativeNumberValidator } from "./validators/number/negative_number.ts";
Expand Down Expand Up @@ -64,7 +65,9 @@ export const enumerate = /* @__PURE__ */ ctorFn(
export const instance = /* @__PURE__ */ ctorFn(
/* @__PURE__ */ bind(InstanceValidator).expect(message1).build(),
);

export const prototype = /* @__PURE__ */ ctorFn(
/* @__PURE__ */ bind(PrototypeValidator).expect(shouldBeBut).build(),
);
export const object = /* @__PURE__ */ ctorFn(DictionaryValidator);
export const optional = /* @__PURE__ */ ctorFn(OptionalValidator);
export const nullish = /* @__PURE__ */ new NullishValidator();
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export {
or,
pattern,
positive,
prototype,
single,
string,
symbol,
Expand Down
21 changes: 21 additions & 0 deletions validators/prototype.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// deno-lint-ignore-file ban-types
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

import { ScalarValidator } from "../utils.ts";

export class PrototypeValidator extends ScalarValidator<{}> {
constructor(public proto: {}) {
super();
}

is(input: {}): input is {} {
const is = Object.getPrototypeOf(input) === this.proto;

return is;
}

override toString(): string {
return `prototype of ${Object.getPrototypeOf(this.proto)}`;
}
}

0 comments on commit e031ab5

Please sign in to comment.