Skip to content

Commit 8b88031

Browse files
committed
Address latest review comment
1 parent 8b4208b commit 8b88031

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

lib/utils.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ function validateOpts (opts) {
3535
return opts
3636
}
3737

38-
function inferJSONSchemaType (type, isNonNull, inferCustomType) {
38+
function inferJSONSchemaType (type, isNonNull, customTypeInferenceFn) {
39+
if (customTypeInferenceFn) {
40+
const customResponse = customTypeInferenceFn(type, isNonNull)
41+
if (customResponse) {
42+
return customResponse
43+
}
44+
}
3945
if (type === GraphQLString) {
4046
return isNonNull ? { type: 'string' } : { type: ['string', 'null'] }
4147
}
@@ -45,9 +51,6 @@ function inferJSONSchemaType (type, isNonNull, inferCustomType) {
4551
if (type === GraphQLFloat) {
4652
return isNonNull ? { type: 'number' } : { type: ['number', 'null'] }
4753
}
48-
if (inferCustomType) {
49-
return inferCustomType(type, isNonNull)
50-
}
5154
return {}
5255
}
5356

lib/validators/json-schema-validator.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const { getTypeInfo, inferJSONSchemaType } = require('../utils')
1212
class JSONSchemaValidator extends Validator {
1313
[kValidationSchema] (type, namedType, isNonNull, typeValidation, id) {
1414
let builtValidationSchema = {
15-
...inferJSONSchemaType(namedType, isNonNull, this[kOpts].inferCustomType),
15+
...inferJSONSchemaType(namedType, isNonNull, this[kOpts].customTypeInferenceFn),
1616
$id: id
1717
}
1818

@@ -26,7 +26,7 @@ class JSONSchemaValidator extends Validator {
2626
}
2727
// If we have an array of scalars, set the array type and infer the items
2828
} else if (isListType(type)) {
29-
let items = { ...inferJSONSchemaType(namedType, isNonNull, this[kOpts].inferCustomType), ...builtValidationSchema.items }
29+
let items = { ...inferJSONSchemaType(namedType, isNonNull, this[kOpts].customTypeInferenceFn), ...builtValidationSchema.items }
3030
if (typeValidation !== null) {
3131
items = { ...items, ...typeValidation.items }
3232
}

test/json-schema-validation.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2091,7 +2091,7 @@ t.test('JSON Schema validators', t => {
20912091
})
20922092
})
20932093

2094-
t.test('should invoke inferCustomType option and not affect operations when everything is okay', async (t) => {
2094+
t.test('should invoke customTypeInferenceFn option and not affect operations when everything is okay', async (t) => {
20952095
const productSchema = `
20962096
type Product {
20972097
id: ID!
@@ -2161,11 +2161,10 @@ t.test('JSON Schema validators', t => {
21612161
}
21622162
}
21632163
},
2164-
inferCustomType: (type, isNonNull) => {
2164+
customTypeInferenceFn: (type, isNonNull) => {
21652165
if (type === GraphQLBoolean) {
21662166
return isNonNull ? { type: 'boolean' } : { type: ['boolean', 'null'] }
21672167
}
2168-
return {}
21692168
}
21702169
})
21712170

0 commit comments

Comments
 (0)