Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow nullable input objects and arrays #67

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/validators/json-schema-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ class JSONSchemaValidator extends Validator {
if (isInputObjectType(namedType)) {
if (isListType(type)) {
const items = { ...builtValidationSchema.items, $ref: `https://mercurius.dev/validation/${namedType.name}` }
builtValidationSchema = { ...builtValidationSchema, type: 'array', items }
builtValidationSchema = { ...builtValidationSchema, type: 'array', items, nullable: !isNonNull }
} else {
builtValidationSchema = { ...builtValidationSchema, type: 'object', $ref: `https://mercurius.dev/validation/${namedType.name}` }
builtValidationSchema = {
...builtValidationSchema,
type: 'object',
$ref: `https://mercurius.dev/validation/${namedType.name}`,
nullable: !isNonNull
}
}
// If we have an array of scalars, set the array type and infer the items
} else if (isListType(type)) {
let items = { ...inferJSONSchemaType(namedType, isNonNull), ...builtValidationSchema.items }
if (typeValidation !== null) {
items = { ...items, ...typeValidation.items }
}
builtValidationSchema = { ...builtValidationSchema, type: 'array', items }
builtValidationSchema = { ...builtValidationSchema, type: 'array', items, nullable: !isNonNull }
}

// Merge with existing validation
Expand Down Expand Up @@ -106,6 +111,7 @@ class JSONSchemaValidator extends Validator {
let typeValidationSchema = {
$id: `https://mercurius.dev/validation/${typeName}`,
type: 'object',
nullable: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it's better to define nullability on the usage rather than the definition itself so we have a validation schema that fully aligns with the GraphQL schema, WDYT?

Copy link
Author

@peterc1731 peterc1731 Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this we are technically already still defining nullability on the definition, just implicitly setting it to false, so even if the validation schema contains nullable: true at the point of usage it will fail validation when we send null because the definition itself is non-nullable.

I'm not sure it would be possible to allow objects to be null without this part, unless you have any other ideas how we could do it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could maybe do it with an anyOf in this function: https://github.com/mercurius-js/validation/pull/67/files#diff-ee1a5eaed8c16e887653f519f05ddf10825fb4acfe4320521cda3e701825046dR25

That way we only set the nullability when it's applicable - wdyt?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh that is a good idea, I'll try it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the long wait getting back to you on this!

I've done some investigation and anyOf/oneOf in the spot you described does work to allow input objects to be null without needing this line, but it introduces a a few different issues that I think make this approach unviable.

Firstly, if the object that's being validated has failed on any of the nested fields, it will fail the anyOf check, as it doesn't exactly match the defined object or null, so we get back a lot of extra noise in the error response describing the real issue as nested underneath the failed anyOf check, which I think makes the error harder to understand.

Secondly, if there is a nullable object nested inside the input object, it seems to not be running validation on the fields inside that nested object. I wonder if a conditional schema check has some kind of depth limit? Or possibly I'm missing something with the implementation, but I've tried experimenting with a few similar approaches (like if/else/then) and haven't got this case to work correctly.

I'm all out of ideas for trying to avoid this line, but if you have any more suggestions please let me know and I'm happy to try them out!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I comprehend the logic behind the removal of L114, but I'm not entirely clear on the significance of opting for oneOf over nullable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for my late reply on this! If the approach isn't viable, I think it's okay to go with your original approach as we are protected by the underlying GraphQL validation :) do you agree with this @mcollina ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got some time this weekend, so gonna have a closer look at this one then :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would love to eventually get this PR merged if you have any time to follow up @jonnydgreen or @mcollina !

properties: {}
}

Expand Down
15 changes: 10 additions & 5 deletions test/advanced-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ t.test('Advanced', t => {
$id: 'https://mercurius.dev/validation/ArrayFilters/values',
items: {
type: ['string', 'null']
}
},
nullable: true
},
data: []
}
Expand Down Expand Up @@ -224,7 +225,8 @@ t.test('Advanced', t => {
$id: 'https://mercurius.dev/validation/Query/messages/arrayScalarFilters',
items: {
type: ['string', 'null']
}
},
nullable: true
},
data: [
''
Expand All @@ -245,7 +247,8 @@ t.test('Advanced', t => {
type: 'array',
items: {
$ref: 'https://mercurius.dev/validation/ArrayFilters'
}
},
nullable: true
},
data: []
}
Expand Down Expand Up @@ -553,7 +556,8 @@ t.test('Advanced', t => {
$id: 'https://mercurius.dev/validation/Query/messages/arrayScalarFilters',
items: {
type: ['string', 'null']
}
},
nullable: true
},
data: [
''
Expand All @@ -574,7 +578,8 @@ t.test('Advanced', t => {
type: 'array',
items: {
$ref: 'https://mercurius.dev/validation/ArrayFilters'
}
},
nullable: true
},
data: []
}
Expand Down
25 changes: 18 additions & 7 deletions test/directive-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ t.test('With directives', t => {
items: {
$ref: 'https://mercurius.dev/validation/ArrayFilters'
},
nullable: true,
minItems: 2
},
data: [{ filters: [{ text: '' }] }]
Expand Down Expand Up @@ -848,7 +849,8 @@ t.test('With directives', t => {
minItems: 2,
items: {
type: 'string'
}
},
nullable: false
},
data: [
''
Expand All @@ -869,6 +871,7 @@ t.test('With directives', t => {
items: {
$ref: 'https://mercurius.dev/validation/ArrayFilters'
},
nullable: false,
minItems: 2
},
data: [{ values: [''], filters: [{ text: '' }] }]
Expand Down Expand Up @@ -1080,6 +1083,7 @@ t.test('With directives', t => {
minProperties: 1,
$id: 'https://mercurius.dev/validation/Filters',
type: 'object',
nullable: true,
properties: {
text: {
type: ['string', 'null'],
Expand Down Expand Up @@ -1173,6 +1177,7 @@ t.test('With directives', t => {
minProperties: 2,
$id: 'https://mercurius.dev/validation/Filters',
type: 'object',
nullable: true,
properties: {
text: {
minLength: 1,
Expand Down Expand Up @@ -2210,7 +2215,8 @@ t.test('With directives', t => {
maxProperties: 1,
$id: 'https://mercurius.dev/validation/Query/messages/filters',
type: 'object',
$ref: 'https://mercurius.dev/validation/Filters'
$ref: 'https://mercurius.dev/validation/Filters',
nullable: true
},
data: {
id: '',
Expand Down Expand Up @@ -2302,7 +2308,8 @@ t.test('With directives', t => {
minProperties: 1,
$id: 'https://mercurius.dev/validation/Query/messages/filters',
type: 'object',
$ref: 'https://mercurius.dev/validation/Filters'
$ref: 'https://mercurius.dev/validation/Filters',
nullable: true
},
data: {}
}
Expand Down Expand Up @@ -2386,7 +2393,8 @@ t.test('With directives', t => {
maxItems: 1,
$id: 'https://mercurius.dev/validation/Query/messages/ids',
type: 'array',
items: {}
items: {},
nullable: true
},
data: [
'1',
Expand Down Expand Up @@ -2473,7 +2481,8 @@ t.test('With directives', t => {
minItems: 1,
$id: 'https://mercurius.dev/validation/Query/messages/ids',
type: 'array',
items: {}
items: {},
nullable: true
},
data: []
}
Expand Down Expand Up @@ -2558,7 +2567,8 @@ t.test('With directives', t => {
uniqueItems: true,
$id: 'https://mercurius.dev/validation/Query/messages/ids',
type: 'array',
items: {}
items: {},
nullable: true
},
data: [
'1',
Expand Down Expand Up @@ -2651,7 +2661,8 @@ t.test('With directives', t => {
type: 'integer'
},
$id: 'https://mercurius.dev/validation/Query/messages/ids',
type: 'array'
type: 'array',
nullable: true
},
data: [
'1.1'
Expand Down
49 changes: 43 additions & 6 deletions test/json-schema-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ t.test('JSON Schema validators', t => {
id
text
}
messages(filters: { text: "hello"}, nestedFilters: { input: { text: "hello"} }) {
messages(
filters: { text: "hello"},
nestedFilters: { input: { text: "hello"} },
arrayScalarFilters: ["hello"],
arrayObjectFilters: [{
values: ["hello"]
filters: [{ text: "hello" }]
}]
) {
id
text
}
Expand Down Expand Up @@ -456,6 +464,7 @@ t.test('JSON Schema validators', t => {
type: 'string',
minLength: 1
},
nullable: true,
minItems: 2
},
data: ['']
Expand Down Expand Up @@ -555,6 +564,7 @@ t.test('JSON Schema validators', t => {
items: {
$ref: 'https://mercurius.dev/validation/ArrayFilters'
},
nullable: true,
minItems: 2
},
data: [{ filters: [{ text: '' }] }]
Expand Down Expand Up @@ -767,7 +777,8 @@ t.test('JSON Schema validators', t => {
minItems: 2,
items: {
type: 'string'
}
},
nullable: false
},
data: [
''
Expand All @@ -788,6 +799,7 @@ t.test('JSON Schema validators', t => {
items: {
$ref: 'https://mercurius.dev/validation/ArrayFilters'
},
nullable: false,
minItems: 2
},
data: [{ values: [''], filters: [{ text: '' }] }]
Expand Down Expand Up @@ -956,6 +968,7 @@ t.test('JSON Schema validators', t => {
minProperties: 1,
$id: 'https://mercurius.dev/validation/Filters',
type: 'object',
nullable: true,
properties: {
text: {
type: ['string', 'null'],
Expand Down Expand Up @@ -1040,6 +1053,7 @@ t.test('JSON Schema validators', t => {
minProperties: 2,
$id: 'https://mercurius.dev/validation/Filters',
type: 'object',
nullable: true,
properties: {
text: {
minLength: 1,
Expand Down Expand Up @@ -1351,6 +1365,7 @@ t.test('JSON Schema validators', t => {
items: {
$ref: 'https://mercurius.dev/validation/ArrayFilters'
},
nullable: true,
minItems: 2
},
data: [
Expand Down Expand Up @@ -1590,6 +1605,7 @@ t.test('JSON Schema validators', t => {
items: {
$ref: 'https://mercurius.dev/validation/ArrayFilters'
},
nullable: true,
minItems: 2
},
data: [
Expand Down Expand Up @@ -1830,6 +1846,7 @@ t.test('JSON Schema validators', t => {
items: {
$ref: 'https://mercurius.dev/validation/ArrayFilters'
},
nullable: true,
minItems: 2
},
data: [
Expand Down Expand Up @@ -2059,9 +2076,22 @@ t.test('JSON Schema validators', t => {
t.teardown(app.close.bind(app))

app.register(mercurius, {
schema: `type Query {
nullableInput(input: String): String
}`,
schema: `
input TestObject {
value: Float!
}

type Query {
nullableInput(
stringInput: String,
floatInput: Float,
intInput: Int,
objectInput: TestObject,
arrayInput: [String!],
objectArrayInput: [TestObject!]
): String
}
`,
resolvers: {
Query: {
nullableInput: async (_, { input }) => {
Expand All @@ -2073,7 +2103,14 @@ t.test('JSON Schema validators', t => {
app.register(mercuriusValidation)

const query = `query {
nullableInput(input: null)
nullableInput(
stringInput: null,
floatInput: null,
intInput: null,
objectInput: null,
arrayInput: null,
objectArrayInput: null
)
}`

const response = await app.inject({
Expand Down
Loading