Skip to content

Commit 0e652f7

Browse files
committed
Add new validators
1 parent b71dc5b commit 0e652f7

File tree

6 files changed

+134
-2
lines changed

6 files changed

+134
-2
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.5",
3+
"version": "2.4.0",
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+
import { ValidatorFunction } from "../.."
4+
5+
export const isArray = <V extends ValidatorFunction<unknown, O>, O>(
6+
innerValidator: V
7+
) => async (input: unknown): Promise<Array<O>> => {
8+
if (!Array.isArray(input)) {
9+
throw new ValidationError(
10+
"IS_ARRAY",
11+
"Value not an array",
12+
"This value must be an array."
13+
)
14+
}
15+
16+
let validatedArray: Array<O> = await Promise.all(input.map(innerValidator))
17+
18+
return validatedArray
19+
}

src/validation/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export * from "./drivers/helpers/either"
77
export * from "./drivers/sanitizers/defaultValue"
88
export * from "./drivers/sanitizers/normalizeEmail"
99

10+
export * from "./drivers/validators/isArray"
1011
export * from "./drivers/validators/isBoolean"
12+
export * from "./drivers/validators/isExactly"
1113
export * from "./drivers/validators/isISOCountry"
1214
export * from "./drivers/validators/isISODate"
1315
export * from "./drivers/validators/isNull"

tests/helpers/ExpectToThrowCustomClass.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ export const expectToThrowCustomClass = (
1212
expect(e).to.be.instanceOf(classContructor)
1313
}
1414
}
15+
16+
export const expectToThrowCustomClassAsync = async (
17+
candidateFunction: Function,
18+
classContructor: ConstructorOf<any>
19+
) => {
20+
try {
21+
await candidateFunction()
22+
expect(false).to.equal(true)
23+
} catch (e) {
24+
expect(e).to.be.instanceOf(classContructor)
25+
}
26+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import {
2+
expectToThrowCustomClass,
3+
expectToThrowCustomClassAsync,
4+
} from "../../../helpers/ExpectToThrowCustomClass"
5+
import { expect } from "chai"
6+
import { either } from "../../../../src/validation/drivers/helpers/either"
7+
import { isArray } from "../../../../src/validation/drivers/validators/isArray"
8+
import {
9+
ValidationError,
10+
isBoolean,
11+
isNumber,
12+
isObject,
13+
isString,
14+
isUndefined,
15+
} from "../../../../src"
16+
17+
describe("isArray", () => {
18+
it("should return the validated array if all values are valid", async () => {
19+
let testValidator = isArray(
20+
isObject({
21+
test: isString,
22+
otherTest: isNumber,
23+
optionalTest: either(isUndefined, isBoolean),
24+
})
25+
)
26+
27+
expect(
28+
await testValidator([
29+
{
30+
test: "test",
31+
otherTest: 15,
32+
},
33+
{
34+
test: "more test",
35+
otherTest: 15,
36+
optionalTest: false,
37+
},
38+
])
39+
).to.deep.equal([
40+
{
41+
test: "test",
42+
otherTest: 15,
43+
},
44+
{
45+
test: "more test",
46+
otherTest: 15,
47+
optionalTest: false,
48+
},
49+
])
50+
51+
expect(await testValidator([])).to.deep.equal([])
52+
})
53+
54+
it("should return a validation error if input is not boolean", async () => {
55+
let testValidator = isArray(
56+
isObject({
57+
test: isString,
58+
otherTest: isNumber,
59+
optionalTest: either(isUndefined, isBoolean),
60+
})
61+
)
62+
63+
await expectToThrowCustomClassAsync(
64+
async () => await testValidator({}),
65+
ValidationError
66+
)
67+
await expectToThrowCustomClassAsync(
68+
async () => await testValidator("false"),
69+
ValidationError
70+
)
71+
await expectToThrowCustomClassAsync(
72+
async () => await testValidator("true"),
73+
ValidationError
74+
)
75+
await expectToThrowCustomClassAsync(
76+
async () => await testValidator(false),
77+
ValidationError
78+
)
79+
await expectToThrowCustomClassAsync(
80+
async () => await testValidator([{}]),
81+
ValidationError
82+
)
83+
await expectToThrowCustomClassAsync(
84+
async () =>
85+
await testValidator([
86+
{
87+
test: "test",
88+
otherTest: 15,
89+
},
90+
{
91+
test: "test",
92+
otherTest: 15,
93+
optionalTest: "wrong",
94+
},
95+
]),
96+
ValidationError
97+
)
98+
})
99+
})

tests/validation/drivers/validators/isExactly.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe("isExactly", () => {
1212
expect(testValidator("more-test")).to.equal("more-test")
1313
})
1414

15-
it("should return a validation error if input is not boolean", () => {
15+
it("should return a validation error if input is not in the allowed set", () => {
1616
let testValidator = isExactly(["test", "other-test", "more-test"])
1717

1818
expectToThrowCustomClass(() => testValidator({}), ValidationError)

0 commit comments

Comments
 (0)