validate-custom-fields-interceptor.ts 5.3 KB

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