Skip to content

Commit b71dc5b

Browse files
committed
Implement isExactly validator
1 parent 33e00c6 commit b71dc5b

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "strontium",
3-
"version": "2.3.4",
3+
"version": "2.3.5",
44
"description": "Strontium is a TypeScript toolkit for High Performance API servers built for Production not Projects.",
55
"main": "lib/src/index.js",
66
"types": "lib/src/index.d.ts",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ValidationError } from "../../../errors"
2+
3+
export const isExactly = <V extends Array<O>, O>(values: V) => (
4+
i: unknown
5+
): O => {
6+
for (let value of values) {
7+
if (value === i) {
8+
return value
9+
}
10+
}
11+
12+
throw new ValidationError(
13+
"IS_EXACTLY",
14+
"Value not in permitted set",
15+
`The provided value was not allowed for this field. Allowed values are: '${values.join(
16+
"', '"
17+
)}'`
18+
)
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { expectToThrowCustomClass } from "../../../helpers/ExpectToThrowCustomClass"
2+
import { expect } from "chai"
3+
import { isExactly } from "../../../../src/validation/drivers/validators/isExactly"
4+
import { ValidationError, isBoolean } from "../../../../src"
5+
6+
describe("isExactly", () => {
7+
it("should return the input if the input is included in the allowed set", () => {
8+
let testValidator = isExactly(["test", "other-test", "more-test"])
9+
10+
expect(testValidator("other-test")).to.equal("other-test")
11+
expect(testValidator("test")).to.equal("test")
12+
expect(testValidator("more-test")).to.equal("more-test")
13+
})
14+
15+
it("should return a validation error if input is not boolean", () => {
16+
let testValidator = isExactly(["test", "other-test", "more-test"])
17+
18+
expectToThrowCustomClass(() => testValidator({}), ValidationError)
19+
expectToThrowCustomClass(() => testValidator(1), ValidationError)
20+
expectToThrowCustomClass(() => testValidator(0), ValidationError)
21+
expectToThrowCustomClass(() => testValidator("das"), ValidationError)
22+
expectToThrowCustomClass(() => testValidator("true"), ValidationError)
23+
expectToThrowCustomClass(() => testValidator(false), ValidationError)
24+
expectToThrowCustomClass(() => testValidator([]), ValidationError)
25+
})
26+
})

0 commit comments

Comments
 (0)