Skip to content

Commit

Permalink
feat(range): add validator for range
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 24, 2023
1 parent 2bde8c0 commit 77e3004
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions combinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// This module is browser compatible.

import { PropertyValidator } from "./validators/property.ts";
import { RangeValidator } from "./validators/range.ts";
import { FixedArrayValidator } from "./validators/array/fixed_array.ts";
import { DictionaryValidator } from "./validators/object/dictionary.ts";
import { OptionalValidator } from "./validators/object/optional.ts";
Expand Down Expand Up @@ -48,6 +49,7 @@ export const ne = /* @__PURE__ */ lazy(InequalityValidator);
export const not = /* @__PURE__ */ lazy(NotValidator);
export const or = /* @__PURE__ */ lazy(OrValidator);
export const and = /* @__PURE__ */ lazy(AndValidator);
export const between = /* @__PURE__ */ lazy(RangeValidator);

// known
export const property = /* @__PURE__ */ lazy(PropertyValidator);
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export {
} from "./validation.ts";
export {
and,
between,
bigint,
boolean,
count,
Expand Down
39 changes: 39 additions & 0 deletions validators/range.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

import { isInRange, shouldBeBut } from "./utils.ts";
import { ScalarValidator } from "../utils.ts";

export class RangeValidator<In> extends ScalarValidator<In> {
#range!: [In, In];

get range(): [In, In] {
return this.#range;
}

set range(range) {
if (range[1] <= range[0]) {
throw RangeError("max should be greater than min");
}

this.#range = range;
}

/**
* @throws {RangeError} If {@link max} less than or equal to {@link min}.
*/
constructor(min: In, max: In) {
super();
super.expect(shouldBeBut);

this.range = [min, max];
}

override is(input: In): input is In {
return isInRange(input, this.range);
}

override toString(): string {
return `between ${this.range[0]} and ${this.range[1]}`;
}
}
14 changes: 14 additions & 0 deletions validators/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@ export function shouldBeBut(
): string {
return interpolate(error.should_be_but, [this, input]);
}

/**
* @throws {RangeError} If max less than or equal to min.
*/
export function isInRange<T>(
input: T,
range: readonly [mix: T, max: T],
): boolean {
const [min, max] = range;

if (max <= min) throw new RangeError("max should be greater then min");

return min <= input && input <= max;
}

0 comments on commit 77e3004

Please sign in to comment.