Files
klz-cables.com/.pnpm-store/v10/files/53/bb724052c5a54b23c4d87fdc6ca38d211f81859e62a535c69a0431f2d89fad375c970501da39492eff91c31c97e3ceb68d668eccc5afb7f9094df03aa5b08d
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

57 lines
2.3 KiB
Plaintext

import { GraphQLScalarType, Kind } from 'graphql';
import { createGraphQLError } from '../error.js';
const LOCAL_DATE_FORMAT = /^\d{4}-\d{2}-\d{2}$/;
function validateLocalDate(value, ast) {
if (typeof value !== 'string') {
throw createGraphQLError(`Value is not string: ${value}`, ast ? { nodes: ast } : undefined);
}
// check that it's in the `yyyy-MM-dd` format
const isValidFormat = LOCAL_DATE_FORMAT.test(value);
if (!isValidFormat) {
throw createGraphQLError(`Value is not a valid LocalDate: ${value}`, ast ? { nodes: ast } : undefined);
}
// check that it appears to be a valid date, e.g., not something like `2020-13-46`
const valueAsDate = new Date(value);
const isValidDate = !isNaN(valueAsDate.getTime());
if (!isValidDate) {
throw createGraphQLError(`Value is not a valid LocalDate: ${value}`, ast ? { nodes: ast } : undefined);
}
// some additional logic to catch invalid dates like `2020-02-30`
// that we catch by serializing the Date object into an ISO string and checking that our serialized date matches
// the original value
const isCalendarDate = valueAsDate.toISOString() === `${value}T00:00:00.000Z`;
if (!isCalendarDate) {
throw createGraphQLError(`Value is not a valid LocalDate: ${value}`, ast ? { nodes: ast } : undefined);
}
return value;
}
export const GraphQLLocalDate = /*#__PURE__*/ new GraphQLScalarType({
name: 'LocalDate',
description: 'A local date string (i.e., with no associated timezone) in `YYYY-MM-DD` format, e.g. `2020-01-01`.',
serialize(value) {
// value sent to client as string
return validateLocalDate(value);
},
parseValue(value) {
// value from client as json
return validateLocalDate(value);
},
parseLiteral(ast) {
// value from client in ast
if (ast.kind !== Kind.STRING) {
throw createGraphQLError(`Can only validate strings as local dates but got a: ${ast.kind}`, {
nodes: ast,
});
}
return validateLocalDate(ast.value, ast);
},
extensions: {
codegenScalarType: 'string',
jsonSchema: {
title: 'LocalDate',
type: 'string',
pattern: LOCAL_DATE_FORMAT.source,
},
},
});