Files
klz-cables.com/.pnpm-store/v10/files/35/998b896277ed7e271c6b5690fec364950940571a2d6f659288471044d542ba4cbdbc5f4c808fb70569ca1b513a4abf591556187676b6737cd1847624e1e0a1
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

46 lines
1.9 KiB
Plaintext

import { GraphQLScalarType, Kind } from 'graphql';
import { createGraphQLError } from './error.js';
export class RegularExpression extends GraphQLScalarType {
constructor(name, regex, options = {}) {
const errorMessage = options.errorMessage
? options.errorMessage
: (r, v) => `Value does not match ${r}: ${v}`;
super({
name,
description: options.description || `A field whose value matches ${regex}.`,
serialize(value) {
if (value != null && !regex.test(value.toString())) {
throw createGraphQLError(errorMessage(regex, value));
}
return value;
},
parseValue(value) {
if (value != null && !regex.test(value === null || value === void 0 ? void 0 : value.toString())) {
throw createGraphQLError(errorMessage(regex, value));
}
return value;
},
parseLiteral(ast) {
if (ast.kind === Kind.NULL) {
return null;
}
if (options.stringOnly && ast.kind !== Kind.STRING) {
throw createGraphQLError(`Can only validate strings as ${name} but got a: ${ast.kind}`);
}
if (!('value' in ast) || ast.kind === Kind.ENUM) {
throw createGraphQLError(`Can only validate primitive values as ${name} but got a: ${ast.kind}`, {
nodes: [ast],
});
}
if (ast.value != null && !regex.test(ast.value.toString())) {
throw createGraphQLError(errorMessage(regex, ast.value), { nodes: ast });
}
return ast.value;
},
extensions: {
codegenScalarType: options.stringOnly ? 'string' : 'string | number | boolean',
},
});
}
}