@eslint-zod/utils
v5.3.0
Published
Shared utilities for eslint-zod plugins
Maintainers
Readme
@eslint-zod/utils
Shared AST utilities for
eslint-plugin-zodeslint-plugin-zod-minieslint-plugin-zod-core
[!NOTE] This package is a dependency of all previously listed packages, so you do not need to install it directly.
API
Every export carries JSDoc with usage notes — refer to the linked source files (or hover in your editor) for the full description and examples. This README only lists what's available.
Root exports — @eslint-zod/utils
AST parsing, import tracking, traversal, and fixer helpers.
Import tracking & scopes
ZodImportScope— the class defining which import sources a plugin considers in-scope;scope.createTracker({ kind })is how rules get a per-file import tracker;kindis required, andsourceCodeenables the scope-aware resolvers belowzodImportScope,zodMiniImportScope,zodCoreImportScope— pre-builtZodImportScopeinstancesscope.createTracker({ kind, sourceCode? })— the returned tracker exposescreateSchemaVisitor,detectZodSchemaRootNode,isZodSchemaOfType,collectZodChainMethods,collectZodSchemaConstraints, and the import-lookup helpers.getZodImportBindings()returns every binding with the declaration it came from, andresolveZodImport(node)/resolveZodExport(node)resolve a node to its zod import honouring lexical scope, so a shadowedzresolves tonull— use them overisZodNamespacein a rule whose fixer writes an import.createTracker({ kind })picks which specifiers are recorded, and is required —'value'skipsimport type { … }and{ type … },'type'keeps only those,'all'keeps both and is what a rule reading a type position needs, since either form reaches one. Schema detection is reachable only through a tracker — the raw forms need the tracker's private import mapstracker.createSchemaVisitor({ schemaType?, onSchema })— builds the{ ImportDeclaration, CallExpression }visitor a rule returns, with detection and theschemaTypefilter already applied. The standard shape for a schema rule; spread it to add more visitor keys- types
ZodSchemaImportTracker,ZodChainItem,ZodImportBinding,ZodImportKind,ZodSchemaVisitorOptions,ZodTrackerOptions
Schema detection & navigation
findParentSchemaMatchingCondition(node, options)— search up the AST for a matching ancestor schema callgetZodChainedMethodNames(meta)— the methods chained onto a schema, with the factory removed.ZodSchemaMeta.methodsincludes the factory for a namespace schema (z.number().safe()→['number', 'safe']) but not for a named import (number().safe()→['safe']), so a baremethods.includes(name)matches the factory of an aliased import. Use this to ask "does this schema chain method X?"getZodSchemaBaseType(schemaType)— map a schema factory name to its base type category; returnsZodSchemaBaseType | null,nullfor factories rules should not reason about- type
ZodSchemaMeta— what the tracker'sdetectZodSchemaRootNodereturns (ornull) - types
ZodSchemaConstraint,ZodChainedConstraint,ZodCheckArgumentConstraint— the normalized constraints produced by the tracker'scollectZodSchemaConstraints
Fixer helpers
buildZodChainRemoveMethodFix(opts)— remove one method from a chainbuildZodChainReplacementFix(opts)— replace a run of methodsbuildZodConstraintsRemoveFix(opts)— remove a set ofZodSchemaConstraints (chained or check-argument)buildZodWrapperUnwrapFix(opts)— replace a single-argument wrapper call with its argument
Zod vocabulary tables
ZOD_IMMUTABLE_SCHEMA_TYPES— schema factory names whose parsed output is already immutableZOD_MUTATING_CHECK_NAMES— Zod check names that mutate the validated valueZOD_NON_SCHEMA_PRODUCING_METHODS— Zod method names that do not return a schemaZOD_NON_SCHEMA_HELPER_NAMES/isZodNonSchemaHelperCall(meta)— top-levelzexports whose call yields something other than a schema (z.toJSONSchema,z.registry,z.parse); unlike the complement ofisZodSchemaFactoryCallit leaves standalone checks aloneZOD_SCHEMA_FACTORY_NAMES/isZodSchemaFactoryName(name)— every top-level Zod export whose call evaluates to a schema; use the predicate instead of a per-rule factory listisZodSchemaFactoryCall(meta)— whether adetectZodSchemaRootNoderesult builds a schema; resolves theiso/coercenamespaces, and rejects the helpers that only consume a schema (z.toJSONSchema,z.prettifyError)ZOD_STRING_FORMAT_METHODS— deprecatedz.string().<format>()methods and the top-level factory replacing each; typeZodStringFormatMethodNameZOD_STRING_FORMAT_NAMES— top-level string-format factory names that all parse tostringZOD_TYPE_CHANGING_METHODS— chained methods that rebuild the schema around another type (array,or,pipe, …); a chain's earlier checks do not carry past one
Check vocabulary
Canonical names shared by both API styles, so rule logic compares zod's .min(2) with zod/mini's z.minLength(2) as one constraint.
canonicalizeZodConstraintName(constraint, baseType)— reduce a constraint's spelling to its canonical namegetZodCheckDescriptor(canonicalName)— what a check means: which base types accept it and what it bounds- types
ZodCheckDescriptor,ZodCheckBound,ZodCheckDomain
Shared rule builders — @eslint-zod/utils/rule-builders/<rule-name>
Each rule shared between eslint-plugin-zod and eslint-plugin-zod-mini (some also eslint-plugin-zod-core) exposes its create(...) factory from a dedicated subpath. Plugins keep rule metadata local and reuse the runtime logic.
buildConsistentImportCreate(scope)buildConsistentImportSourceCreate(scope)buildConsistentObjectSchemaTypeCreate(scope)buildConsistentSchemaOutputTypeStyleCreate(scope)buildConsistentSchemaVarNameCreate(scope)buildNoAnySchemaCreate(scope)buildNoCoerceBooleanCreate(scope)buildNoConflictingChecksCreate(scope)— also exports theNoConflictingChecksOptionsandNoConflictingChecksMessageIdscontractsbuildNoDuplicateSchemaMethodsCreate(scope, excludedMethods)buildNoDynamicSchemaValueCreate(scope)buildNoEmptyCustomSchemaCreate(scope)buildNoFunctionScopedSchemaCreate(scope)buildNoNativeEnumCreate(scope)buildNoPromiseSchemaCreate(scope)buildNoThrowInRefineCreate(scope)buildNoTransformInRecordKeyCreate(scope, transformNames)—transformNamesare matched against the key schema's factory and its constraints, so one list covers both API stylesbuildNoUnknownSchemaCreate(scope)buildNoUnnecessaryReadonlyCreate(scope)buildPreferEnumOverLiteralUnionCreate(scope)buildPreferNullishCreate(scope)buildPreferTupleOverArrayLengthCreate(scope)buildPreferValidateCreate(scope, api)— success-only parsing suggestions;apiis aZodValidateApiStyle(schema-methodorstandalone)buildRequireBrandTypeParameterCreate(scope)buildRequireErrorMessageCreate(scope)buildSchemaErrorPropertyStyleCreate(scope)
The consistent-import builder additionally re-exports the import-syntax helpers used by its fixer: IMPORT_SYNTAXES, ImportSyntax, isGroupFirstImportKindValidForSyntax, shouldIdentifierBeRenamed, getNamespaceAliasNameFrom.
Rule patterns — @eslint-zod/utils/rule-patterns/<pattern-name>
Rule shapes that recur across several rules, parameterized by the names they differ in. Unlike rule builders these are not tied to one rule name, and a single plugin may use one several times.
buildDeprecatedSchemaPropertyCreate(options)— flag a deprecated property access on a schema of a givenschemaType(z.number().isInt); also exportsDeprecatedSchemaPropertyOptionsbuildDeprecatedSchemaMethodCreate(options)— flag a deprecated method anywhere in a schema chain (.isOptional()); also exportsDeprecatedSchemaMethodOptionsbuildCollapseEqualBoundsCreate(options)— collapse an equal lower/upper bound pair into the exact-form check of the same domain (z.string().min(3).max(3)→z.string().length(3)); also exportsCollapseEqualBoundsOptionsandCollapsibleBoundDomainbuildPreferDedicatedFactoryCreate(options)— prefer a dedicated factory over a general one plus a chained modifier (z.looseObject()overz.object().passthrough()); also exportsPreferDedicatedFactoryOptionsbuildPreferTopLevelFactoryCreate(options)— prefer a top-level factory over a deprecated method chained on a general one (z.uuid()overz.string().uuid()); also exportsPreferTopLevelFactoryOptionsandTopLevelFactoryReplacement
