UNPKG

@pothos/plugin-zod

Version:

A Pothos plugin for adding argument validation

207 lines (206 loc) 8.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: Object.getOwnPropertyDescriptor(all, name).get }); } _export(exports, { get PothosZodPlugin () { return PothosZodPlugin; }, get createZodSchema () { return _createZodSchema.default; }, get default () { return _default; } }); require("./global-types"); const _core = /*#__PURE__*/ _interop_require_wildcard(require("@pothos/core")); const _zod = /*#__PURE__*/ _interop_require_wildcard(require("zod")); const _createZodSchema = /*#__PURE__*/ _interop_require_wildcard(require("./createZodSchema")); _export_star(require("./types"), exports); function _define_property(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _export_star(from, to) { Object.keys(from).forEach(function(k) { if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) { Object.defineProperty(to, k, { enumerable: true, get: function() { return from[k]; } }); } }); return from; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interop_require_wildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = { __proto__: null }; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for(var key in obj){ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } const pluginName = 'zod'; class PothosZodPlugin extends _core.BasePlugin { onInputFieldConfig(fieldConfig) { const fieldType = (0, _core.resolveInputTypeConfig)(fieldConfig.type, this.buildCache); const validationOptions = fieldConfig.pothosOptions.validate; if (!validationOptions && fieldType.kind !== 'InputObject') { return fieldConfig; } const fieldName = fieldConfig.kind === 'Arg' ? `${fieldConfig.parentType}.${fieldConfig.parentField}(${fieldConfig.name})` : `${fieldConfig.parentType}.${fieldConfig.name}`; const validator = this.createValidator(validationOptions, fieldConfig.type, fieldName); if (fieldConfig.kind === 'Arg') { return { ...fieldConfig, extensions: { ...fieldConfig.extensions, validator } }; } this.inputFieldValidators.set(fieldConfig.parentType, { ...this.inputFieldValidators.get(fieldConfig.parentType), [fieldConfig.name]: validator }); return fieldConfig; } wrapResolve(resolver, fieldConfig) { var _this_builder_options_zod; // Only used to check if validation is required const argMap = (0, _core.mapInputFields)(fieldConfig.args, this.buildCache, (field)=>{ var _field_extensions; var _field_extensions_validator; return (_field_extensions_validator = (_field_extensions = field.extensions) === null || _field_extensions === void 0 ? void 0 : _field_extensions.validator) !== null && _field_extensions_validator !== void 0 ? _field_extensions_validator : null; }, this.mappingCache); if (!argMap && !fieldConfig.pothosOptions.validate) { return resolver; } const args = {}; for (const [argName, arg] of Object.entries(fieldConfig.args)){ var _arg_extensions; const validator = (_arg_extensions = arg.extensions) === null || _arg_extensions === void 0 ? void 0 : _arg_extensions.validator; if (validator) { args[argName] = validator; } } let validator = _zod.object(args).passthrough(); if (fieldConfig.pothosOptions.validate) { validator = (0, _createZodSchema.refine)(validator, fieldConfig.pothosOptions.validate); } const validationError = (_this_builder_options_zod = this.builder.options.zod) === null || _this_builder_options_zod === void 0 ? void 0 : _this_builder_options_zod.validationError; const validatorWithErrorHandling = validationError && async function validate(value, ctx, info) { try { const result = await validator.parseAsync(value); return result; } catch (error) { const errorOrMessage = validationError(error, value, ctx, info); if (typeof errorOrMessage === 'string') { throw new _core.PothosValidationError(errorOrMessage); } throw errorOrMessage; } }; return async (parent, rawArgs, context, info)=>resolver(parent, await (validatorWithErrorHandling ? validatorWithErrorHandling(rawArgs, context, info) : validator.parseAsync(rawArgs)), context, info); } createValidator(optionsOrConstraint, type, fieldName) { const options = Array.isArray(optionsOrConstraint) || typeof optionsOrConstraint === 'function' ? { refine: optionsOrConstraint } : optionsOrConstraint; if ((type === null || type === void 0 ? void 0 : type.kind) === 'InputObject') { const typeConfig = this.buildCache.getTypeConfig(type.ref, 'InputObject'); let fieldValidator = (0, _createZodSchema.refine)(_zod.lazy(()=>{ var _this_inputFieldValidators_get; return _zod.object((_this_inputFieldValidators_get = this.inputFieldValidators.get(typeConfig.name)) !== null && _this_inputFieldValidators_get !== void 0 ? _this_inputFieldValidators_get : {}).passthrough(); }), options); if (typeConfig.pothosOptions.validate) { fieldValidator = (0, _createZodSchema.refine)(fieldValidator, typeConfig.pothosOptions.validate); } return (0, _createZodSchema.combine)([ fieldValidator ], type.required); } if ((type === null || type === void 0 ? void 0 : type.kind) === 'List') { if (options && !(0, _createZodSchema.isArrayValidator)(options)) { throw new _core.PothosSchemaError(`Expected valid array validator for ${fieldName}`); } const items = this.createValidator(options === null || options === void 0 ? void 0 : options.items, type.type, fieldName); if (options) { return (0, _createZodSchema.combine)([ (0, _createZodSchema.createArrayValidator)(options, items) ], type.required); } return (0, _createZodSchema.combine)([ items.array() ], type.required); } if (!options) { return _zod.unknown(); } return (0, _createZodSchema.default)(options, !type || type.required); } constructor(...args){ super(...args), _define_property(this, "inputFieldValidators", new Map()), _define_property(this, "mappingCache", new Map()); } } _core.default.registerPlugin(pluginName, PothosZodPlugin, { v3 (options) { return { validationOptions: undefined, zod: options.validationOptions }; } }); const _default = pluginName; //# sourceMappingURL=index.js.map