Skip to content

Commit

Permalink
Address latest review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalshareef committed Mar 19, 2024
1 parent 8b4208b commit 8b88031
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
11 changes: 7 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ function validateOpts (opts) {
return opts
}

function inferJSONSchemaType (type, isNonNull, inferCustomType) {
function inferJSONSchemaType (type, isNonNull, customTypeInferenceFn) {
if (customTypeInferenceFn) {
const customResponse = customTypeInferenceFn(type, isNonNull)
if (customResponse) {
return customResponse
}
}
if (type === GraphQLString) {
return isNonNull ? { type: 'string' } : { type: ['string', 'null'] }
}
Expand All @@ -45,9 +51,6 @@ function inferJSONSchemaType (type, isNonNull, inferCustomType) {
if (type === GraphQLFloat) {
return isNonNull ? { type: 'number' } : { type: ['number', 'null'] }
}
if (inferCustomType) {
return inferCustomType(type, isNonNull)
}
return {}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/validators/json-schema-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { getTypeInfo, inferJSONSchemaType } = require('../utils')
class JSONSchemaValidator extends Validator {
[kValidationSchema] (type, namedType, isNonNull, typeValidation, id) {
let builtValidationSchema = {
...inferJSONSchemaType(namedType, isNonNull, this[kOpts].inferCustomType),
...inferJSONSchemaType(namedType, isNonNull, this[kOpts].customTypeInferenceFn),
$id: id
}

Expand All @@ -26,7 +26,7 @@ class JSONSchemaValidator extends Validator {
}
// If we have an array of scalars, set the array type and infer the items
} else if (isListType(type)) {
let items = { ...inferJSONSchemaType(namedType, isNonNull, this[kOpts].inferCustomType), ...builtValidationSchema.items }
let items = { ...inferJSONSchemaType(namedType, isNonNull, this[kOpts].customTypeInferenceFn), ...builtValidationSchema.items }
if (typeValidation !== null) {
items = { ...items, ...typeValidation.items }
}
Expand Down
5 changes: 2 additions & 3 deletions test/json-schema-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2091,7 +2091,7 @@ t.test('JSON Schema validators', t => {
})
})

t.test('should invoke inferCustomType option and not affect operations when everything is okay', async (t) => {
t.test('should invoke customTypeInferenceFn option and not affect operations when everything is okay', async (t) => {
const productSchema = `
type Product {
id: ID!
Expand Down Expand Up @@ -2161,11 +2161,10 @@ t.test('JSON Schema validators', t => {
}
}
},
inferCustomType: (type, isNonNull) => {
customTypeInferenceFn: (type, isNonNull) => {
if (type === GraphQLBoolean) {
return isNonNull ? { type: 'boolean' } : { type: ['boolean', 'null'] }
}
return {}
}
})

Expand Down

0 comments on commit 8b88031

Please sign in to comment.