validate-custom-fields-interceptor.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
  2. import { GqlExecutionContext } from '@nestjs/graphql';
  3. import { LanguageCode } from '@vendure/common/lib/generated-types';
  4. import { assertNever } from '@vendure/common/lib/shared-utils';
  5. import {
  6. DefinitionNode,
  7. GraphQLInputType,
  8. GraphQLList,
  9. GraphQLNonNull,
  10. GraphQLResolveInfo,
  11. GraphQLSchema,
  12. OperationDefinitionNode,
  13. TypeNode,
  14. } from 'graphql';
  15. import { UserInputError } from '../../common/error/errors';
  16. import { ConfigService } from '../../config/config.service';
  17. import {
  18. CustomFieldConfig,
  19. CustomFields,
  20. LocaleStringCustomFieldConfig,
  21. StringCustomFieldConfig,
  22. } from '../../config/custom-field/custom-field-types';
  23. import { parseContext } from '../common/parse-context';
  24. import { RequestContext } from '../common/request-context';
  25. import { REQUEST_CONTEXT_KEY } from '../common/request-context.service';
  26. import { validateCustomFieldValue } from '../common/validate-custom-field-value';
  27. /**
  28. * This interceptor is responsible for enforcing the validation constraints defined for any CustomFields.
  29. * For example, if a custom 'int' field has a "min" value of 0, and a mutation attempts to set its value
  30. * to a negative integer, then that mutation will fail with an error.
  31. */
  32. @Injectable()
  33. export class ValidateCustomFieldsInterceptor implements NestInterceptor {
  34. private readonly inputsWithCustomFields: Set<string>;
  35. constructor(private configService: ConfigService) {
  36. this.inputsWithCustomFields = Object.keys(configService.customFields).reduce((inputs, entityName) => {
  37. inputs.add(`Create${entityName}Input`);
  38. inputs.add(`Update${entityName}Input`);
  39. return inputs;
  40. }, new Set<string>());
  41. }
  42. intercept(context: ExecutionContext, next: CallHandler<any>) {
  43. const { isGraphQL } = parseContext(context);
  44. if (isGraphQL) {
  45. const gqlExecutionContext = GqlExecutionContext.create(context);
  46. const { operation, schema } = gqlExecutionContext.getInfo<GraphQLResolveInfo>();
  47. const variables = gqlExecutionContext.getArgs();
  48. const ctx: RequestContext = gqlExecutionContext.getContext().req[REQUEST_CONTEXT_KEY];
  49. if (operation.operation === 'mutation') {
  50. const inputTypeNames = this.getArgumentMap(operation, schema);
  51. Object.entries(inputTypeNames).forEach(([inputName, typeName]) => {
  52. if (this.inputsWithCustomFields.has(typeName)) {
  53. if (variables[inputName]) {
  54. this.validateInput(typeName, ctx.languageCode, variables[inputName]);
  55. }
  56. }
  57. });
  58. }
  59. }
  60. return next.handle();
  61. }
  62. private validateInput(
  63. typeName: string,
  64. languageCode: LanguageCode,
  65. variableValues?: { [key: string]: any },
  66. ) {
  67. if (variableValues) {
  68. const entityName = typeName.replace(/(Create|Update)(.+)Input/, '$2');
  69. const customFieldConfig = this.configService.customFields[entityName as keyof CustomFields];
  70. if (variableValues.customFields) {
  71. this.validateCustomFieldsObject(customFieldConfig, languageCode, variableValues.customFields);
  72. }
  73. const translations = variableValues.translations;
  74. if (Array.isArray(translations)) {
  75. for (const translation of translations) {
  76. if (translation.customFields) {
  77. this.validateCustomFieldsObject(
  78. customFieldConfig,
  79. languageCode,
  80. translation.customFields,
  81. );
  82. }
  83. }
  84. }
  85. }
  86. }
  87. private validateCustomFieldsObject(
  88. customFieldConfig: CustomFieldConfig[],
  89. languageCode: LanguageCode,
  90. customFieldsObject: { [key: string]: any },
  91. ) {
  92. for (const [key, value] of Object.entries(customFieldsObject)) {
  93. const config = customFieldConfig.find(c => c.name === key);
  94. if (config) {
  95. validateCustomFieldValue(config, value, languageCode);
  96. }
  97. }
  98. }
  99. private getArgumentMap(
  100. operation: OperationDefinitionNode,
  101. schema: GraphQLSchema,
  102. ): { [inputName: string]: string } {
  103. const mutationType = schema.getMutationType();
  104. if (!mutationType) {
  105. return {};
  106. }
  107. const map: { [inputName: string]: string } = {};
  108. for (const selection of operation.selectionSet.selections) {
  109. if (selection.kind === 'Field') {
  110. const name = selection.name.value;
  111. const inputType = mutationType.getFields()[name];
  112. for (const arg of inputType.args) {
  113. map[arg.name] = this.getInputTypeName(arg.type);
  114. }
  115. }
  116. }
  117. return map;
  118. }
  119. private getNamedTypeName(type: TypeNode): string {
  120. if (type.kind === 'NonNullType' || type.kind === 'ListType') {
  121. return this.getNamedTypeName(type.type);
  122. } else {
  123. return type.name.value;
  124. }
  125. }
  126. private getInputTypeName(type: GraphQLInputType): string {
  127. if (type instanceof GraphQLNonNull) {
  128. return this.getInputTypeName(type.ofType);
  129. }
  130. if (type instanceof GraphQLList) {
  131. return this.getInputTypeName(type.ofType);
  132. }
  133. return type.name;
  134. }
  135. }