Code Conventions
March 20, 2026 · View on GitHub
Language & Module System
- TypeScript with
strict: true, targetesnext, modulenodenext. - Source is ESM (
"type": "module"insrc/package.json). All internal imports use.jsextensions (Node ESM resolution). - Imports: use
import type { ... }for type-only imports (enforced by@typescript-eslint/consistent-type-importsvia oxlint). - Import order is enforced by oxfmt
sortImports: builtins, external packages, internal/subpath, relative imports.
Formatting
- oxfmt: 120 char line width, single quotes, trailing commas (
es5), semicolons. Also handles import sorting andpackage.jsonkey sorting. - Lint-staged: on commit, files are auto-formatted with oxfmt and JS/TS files are auto-fixed with oxlint (configured in
lint-staged.config.mjs). - Run
npm run formatto format all files,npm run format:checkto check without modifying. - Run
npm run lintto lint + fix,npm run lint:checkto lint without fixing.
Naming
- Classes:
PascalCase(ZSchema,SchemaCompiler,Report) - Interfaces/types:
PascalCase(JsonSchema,ZSchemaOptions,ValidateError) - Functions/variables:
camelCase - Error codes:
UPPER_SNAKE_CASE(seeErrorsobject insrc/errors.ts) - Unused parameters: prefix with
_(e.g.,_err)
Error Handling
- The library uses a throw-or-return pattern. The base
ZSchema.validate()throwsValidateErroron failure.ZSchemaSafe.validate()catches and returns{ valid, err? }. ValidateErrorextendsErrorwith a.detailsproperty (array ofSchemaErrorDetail).- Errors are defined in the
Errorsobject insrc/errors.tswith template strings ({0},{1}placeholders). - To add a new error code, add it to the
Errorsobject insrc/errors.ts.
Exports
- All public API must be exported through
src/index.ts. - Keep internal types (e.g.,
JsonSchemaInternal, compiler metadata) unexported.
Documentation Comments (TSDoc)
- All public methods and classes must have TSDoc comments (
/** ... */). - Use
@param name - Description,@returns Description,@throws Description,@example. - Do not use JSDoc-style type annotations in tags (e.g.,
@param {string} nameor@throws {Error}) — TypeScript provides the types. - Use
{@link SymbolName}for inline cross-references (e.g.,@throws {@link ValidateError} if validation fails). - Mark internal-only members with
/** @internal */. - Keep descriptions concise — one sentence for simple getters, a short paragraph for complex methods.
Dependencies
- Runtime:
validator(format validation: email, IP, URL). One dependency only — minimize additions. - Optional:
commander(CLI support viabin/z-schema). Not required for library usage. - Avoid adding new runtime dependencies unless absolutely necessary.
Utility Functions
- Pure utility functions go in
src/utils/. Each file is single-purpose. - Current utils:
array.ts,clone.ts,json.ts,properties.ts,schema-regex.ts,symbols.ts,unicode.ts,uri.ts,what-is.ts.