-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(size): add validator for element size
- Loading branch information
1 parent
c870c00
commit dccc945
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { format, ScalarValidator } from "../../utils.ts"; | ||
import error from "../error.json" assert { type: "json" }; | ||
|
||
export class SizeValidator extends ScalarValidator<Iterable<unknown>> { | ||
constructor(public size: number) { | ||
super(); | ||
super.expect(({ input }) => | ||
format(error.should_be_but, this, getSize(input)) | ||
); | ||
} | ||
|
||
override is(input: Iterable<unknown>): boolean { | ||
return getSize(input) === this.size; | ||
} | ||
|
||
toString(): string { | ||
return `size of ${this.size}`; | ||
} | ||
} |