Skip to content

Commit

Permalink
feat(max_count): add validator for maximum count of item
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 20, 2023
1 parent 83e9bbf commit dbbe7ea
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions validators/iterable/max_count.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

import { getSize } from "./utils.ts";
import { interpolate, ScalarValidator } from "../../utils.ts";
import error from "../error.json" assert { type: "json" };

export class MaxCountValidator extends ScalarValidator<Iterable<unknown>> {
constructor(public size: number) {
super();
super.expect(({ input }) =>
interpolate(error.max_count, [size, getSize(input)])
);
}

override is(input: Iterable<unknown>): boolean {
return getSize(input) <= this.size;
}

toString(): string {
return `max item count of ${this.size}`;
}
}

0 comments on commit dbbe7ea

Please sign in to comment.