Skip to content

Commit

Permalink
Validate oneOf types
Browse files Browse the repository at this point in the history
This validates that all fields of oneOf objects are nullable and that
all fields of oneOf input object are nullable and do not have a
default value.
  • Loading branch information
erikkessler1 committed Mar 18, 2022
1 parent 534b243 commit 25928e3
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
65 changes: 64 additions & 1 deletion src/type/__tests__/validation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ describe('Type System: A Schema must have Object root types', () => {
]);
});

it('rejects a schema extended with invalid root types', () => {
it('rejects a Schema extended with invalid root types', () => {
let schema = buildSchema(`
input SomeInputObject {
test: String
Expand Down Expand Up @@ -1663,6 +1663,69 @@ describe('Type System: Input Object fields must have input types', () => {
});
});

describe('Type System: OneOf Object fields must be nullable', () => {
it('rejects non-nullable fields', () => {
const schema = buildSchema(`
type Query {
test: SomeObject
}
type SomeObject @oneOf {
a: String
b: String!
}
`);
expectJSON(validateSchema(schema)).toDeepEqual([
{
message:
'Field SomeObject.b must be nullable as it is part of a OneOf Type.',
locations: [{ line: 8, column: 12 }],
},
]);
});
});

describe('Type System: OneOf Input Object fields must be nullable', () => {
it('rejects non-nullable fields', () => {
const schema = buildSchema(`
type Query {
test(arg: SomeInputObject): String
}
input SomeInputObject @oneOf {
a: String
b: String!
}
`);
expectJSON(validateSchema(schema)).toDeepEqual([
{
message: 'OneOf input field SomeInputObject.b must be nullable.',
locations: [{ line: 8, column: 12 }],
},
]);
});

it('rejects fields with default values', () => {
const schema = buildSchema(`
type Query {
test(arg: SomeInputObject): String
}
input SomeInputObject @oneOf {
a: String
b: String = "foo"
}
`);
expectJSON(validateSchema(schema)).toDeepEqual([
{
message:
'OneOf input field SomeInputObject.b cannot have a default value.',
locations: [{ line: 8, column: 9 }],
},
]);
});
});

describe('Objects must adhere to Interface they implement', () => {
it('accepts an Object which implements an Interface', () => {
const schema = buildSchema(`
Expand Down
42 changes: 42 additions & 0 deletions src/type/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { isEqualType, isTypeSubTypeOf } from '../utilities/typeComparators';

import type {
GraphQLEnumType,
GraphQLField,
GraphQLInputField,
GraphQLInputObjectType,
GraphQLInterfaceType,
Expand Down Expand Up @@ -308,6 +309,23 @@ function validateFields(
);
}
}

if (isObjectType(type) && type.isOneOf) {
validateOneOfObjectField(type, field, context);
}
}
}

function validateOneOfObjectField(
type: GraphQLObjectType,
field: GraphQLField<unknown, unknown, unknown>,
context: SchemaValidationContext,
): void {
if (isNonNullType(field.type)) {
context.reportError(
`Field ${type.name}.${field.name} must be nullable as it is part of a OneOf Type.`,
field.astNode?.type,
);
}
}

Expand Down Expand Up @@ -531,6 +549,30 @@ function validateInputFields(
[getDeprecatedDirectiveNode(field.astNode), field.astNode?.type],
);
}

if (inputObj.isOneOf) {
validateOneOfInputObjectField(inputObj, field, context);
}
}
}

function validateOneOfInputObjectField(
type: GraphQLInputObjectType,
field: GraphQLInputField,
context: SchemaValidationContext,
): void {
if (isNonNullType(field.type)) {
context.reportError(
`OneOf input field ${type.name}.${field.name} must be nullable.`,
field.astNode?.type,
);
}

if (field.defaultValue) {
context.reportError(
`OneOf input field ${type.name}.${field.name} cannot have a default value.`,
field.astNode,
);
}
}

Expand Down

0 comments on commit 25928e3

Please sign in to comment.