1
0

validate-custom-fields-interceptor.ts 5.8 KB

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