graphql-codegen-typescript-validation-schema
Version:
GraphQL Code Generator plugin to generate form validation schema from your GraphQL schema
25 lines (24 loc) • 1.1 kB
JavaScript
/**
* 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()`).
*/
export 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;
}