No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

38 líneas
1.2KB

  1. import { CustomScalar, Scalar } from '@nestjs/graphql';
  2. import { GraphQLError, Kind, ValueNode } from 'graphql';
  3. const validate = (value: string): string => {
  4. const EMAIL_ADDRESS_REGEX = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
  5. if (typeof value !== 'string') {
  6. throw new TypeError(`Value is not string: ${value}`);
  7. }
  8. if (!EMAIL_ADDRESS_REGEX.test(value)) {
  9. throw new TypeError(`Value is not a valid email address: ${value}`);
  10. }
  11. return value;
  12. };
  13. @Scalar('EmailAddress', () => EmailAddress)
  14. export class EmailAddress implements CustomScalar<string, string> {
  15. description = 'A field whose value conforms to the standard internet email address format as specified in RFC822: https://www.w3.org/Protocols/rfc822/.'
  16. parseValue(value: string): string {
  17. return validate(value)
  18. }
  19. serialize(value: string): string {
  20. return validate(value)
  21. }
  22. parseLiteral(ast: ValueNode): string {
  23. if (ast.kind !== Kind.STRING) {
  24. throw new GraphQLError(
  25. `Can only validate strings as email addresses but got a: ${ast.kind}`,
  26. );
  27. }
  28. return validate(ast.value)
  29. }
  30. }