UNPKG

graphql-codegen-typescript-validation-schema

Version:

GraphQL Code Generator plugin to generate form validation schema from your GraphQL schema

28 lines (27 loc) 1.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildScalarSchema = buildScalarSchema; /** * Builds a library-specific scalar schema expression. * * All five validation libraries follow the same pattern: check for a custom * `scalarSchemas` override, fall back to a built-in type map for the resolved * TypeScript type (string/number/boolean), then apply `defaultScalarTypeSchema`, * and finally warn and return the library fallback. * * The only per-library differences are the strings in `typeMap`, the `fallback` * value, and whether custom schemas need wrapping (yup appends `.defined()`). */ function buildScalarSchema(config, visitor, scalarName, options) { if (config.scalarSchemas?.[scalarName]) { const custom = config.scalarSchemas[scalarName]; return options.wrapCustom ? options.wrapCustom(custom) : custom; } const tsType = visitor.getScalarType(scalarName); if (tsType != null && tsType in options.typeMap) return options.typeMap[tsType]; if (config.defaultScalarTypeSchema) return config.defaultScalarTypeSchema; console.warn('unhandled scalar name:', scalarName); return options.fallback; }