Skip to content

Commit 1d59819

Browse files
spukst3rDmitrii Fontanovconfuser
authored
fix: validate falsy numbers (#177)
Co-authored-by: Dmitrii Fontanov <[email protected]> Co-authored-by: James Mortemore <[email protected]>
1 parent 3bf0cfa commit 1d59819

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/query-validation-visitor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class InputObjectValidationVisitor {
278278
validateArrayTypeValue(this.context, valueTypeDef, iFieldTypeDef, value, this.currentField, this.argName, this.variableName, iFieldNameFull)
279279
} else {
280280
// nothing to validate
281-
if (!value && value !== '') return
281+
if (!value && value !== '' && value !== 0) return
282282

283283
validateScalarTypeValue(this.context, this.currentField, iFieldTypeDef, valueTypeDef, value, this.variableName, this.argName, iFieldNameFull, ` at "${iFieldNameFull}"`)
284284
}

test/int.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ module.exports.test = function (setup, implType) {
4949
'Variable "$input" got invalid value 2 at "input.title"' + valueByImplType(implType, '; Expected type "title_Int_NotNull_min_3"') + '. Must be at least 3')
5050
})
5151

52+
it('should fail with input = 0', async function () {
53+
const { body, statusCode } = await this.request
54+
.post('/graphql')
55+
.set('Accept', 'application/json')
56+
.send({ query, variables: { input: { title: 0 } } })
57+
58+
isStatusCodeError(statusCode, implType)
59+
strictEqual(body.errors[0].message,
60+
'Variable "$input" got invalid value 0 at "input.title"' + valueByImplType(implType, '; Expected type "title_Int_NotNull_min_3"') + '. Must be at least 3')
61+
})
62+
5263
if (isSchemaWrapperImplType(implType)) {
5364
it('should throw custom error', async function () {
5465
const request = await setup({ typeDefs: this.typeDefs, formatError })

0 commit comments

Comments
 (0)