Skip to content

Commit f872230

Browse files
fix: validate empty strings (#133)
Co-authored-by: James Mortemore <[email protected]>
1 parent 398e5a5 commit f872230

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
@@ -270,7 +270,7 @@ class InputObjectValidationVisitor {
270270
validateArrayTypeValue(this.context, valueTypeDef, iFieldTypeDef, value, this.currentField, this.argName, this.variableName, iFieldNameFull)
271271
} else {
272272
// nothing to validate
273-
if (!value) return
273+
if (!value && value !== '') return
274274

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

test/string.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 "a💩" at "input.title"' + valueByImplType(implType, '; Expected type "title_String_NotNull_minLength_3"') + '. Must be at least 3 characters in length')
5050
})
5151

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

0 commit comments

Comments
 (0)