Skip to content

Commit 0e39c0d

Browse files
committed
Fix isObject’s detection of undefined keys
1 parent 67f69cd commit 0e39c0d

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
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.3",
3+
"version": "2.3.4",
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",

src/validation/drivers/validators/isObject.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ export const isObject = <V extends ObjectValidator>(validator: V) => async (
1616
let response: Partial<ValidatedObject<V>> = {}
1717

1818
for (let p in validator) {
19-
if (validator.hasOwnProperty(p) && i.hasOwnProperty(p)) {
19+
if (validator.hasOwnProperty(p)) {
2020
// Sadly ts-ignore this as TS doesn't understand that we have strongly ensured
2121
// that this property is present.
2222
// @ts-ignore
2323
let rawValue = i[p]
2424

2525
try {
26-
response[p] = await validator[p](rawValue)
26+
let validatedOutput = await validator[p](rawValue)
27+
28+
if (validatedOutput !== undefined) {
29+
response[p] = validatedOutput
30+
}
2731
} catch (e) {
2832
if (e instanceof ValidationError) {
2933
// Append the path to the error message

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { expect } from "chai"
22
import { combineValidators } from "../../../../src/validation/drivers/helpers/combineValidators"
33
import {
4-
ValidationError,
5-
isISOAlpha2CountryCode,
6-
isObject,
7-
isString,
8-
} from "../../../../src"
4+
ValidationError,
5+
isISOAlpha2CountryCode,
6+
isObject,
7+
isString, isUndefined
8+
} from "../../../../src";
9+
import { either } from "../../../../src/validation/drivers/helpers/either";
910

1011
describe("isNull", () => {
1112
const objectFilter = isObject({
12-
test: isString,
13+
test: either(isString, isUndefined),
1314
otherTest: combineValidators(isString, isISOAlpha2CountryCode),
1415
})
1516

@@ -55,4 +56,16 @@ describe("isNull", () => {
5556
expect(e.constraintName).to.equal("IS_STRING")
5657
}
5758
})
59+
60+
it("should return a required validation error if a key is absent", async () => {
61+
try {
62+
await objectFilter({
63+
test: "Test Value"
64+
})
65+
expect(false).to.equal(true)
66+
} catch (e) {
67+
expect(e).to.be.instanceOf(ValidationError)
68+
expect(e.constraintName).to.equal("IS_STRING")
69+
}
70+
})
5871
})

0 commit comments

Comments
 (0)