Registry / testing / json-schema-to-decoders

json-schema-to-decoders

JSON →
library0.2.6jsnpmunverified

Converts JSON Schema definitions into TypeScript/JavaScript decoder expressions using the decoders.cc library. The package generates source code strings for decoding runtime values according to schema specifications. Version 0.2.6 supports JSON Schema drafts and integrates with the decoders runtime via optional utility library. Key differentiators: CLI and programmatic API, synchronous and asynchronous conversion functions, support for $ref resolution and namespace prefixes. Ideal for projects that already use the decoders library for runtime validation.

npm install json-schema-to-decoders
INSTALL
IMPORT
SIG · JSON-SCHEMA-TO-DEC
J
json-schema-to-decoders
testingjavascriptv0.2.6
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Converter (default)
import converter from 'json-schema-to-decoders';
import { converter } from 'json-schema-to-decoders';
The package uses a default export, not named export. ESM only.
convertSchema
import converter from 'json-schema-to-decoders'; const { convertSchema } = converter;
import { convertSchema } from 'json-schema-to-decoders';
Helper functions like convertSchema are properties of the default export.
convertFile
import converter from 'json-schema-to-decoders'; converter.convertFile('path/to/schema.json');
const { convertFile } = require('json-schema-to-decoders');
The default export is a class; destructuring from it is fine in ESM, but CommonJS require leads to unexpected error.

Demonstrates converting an object schema to decoder code with namespace prefixes and lib references.

import converter from 'json-schema-to-decoders'; const schema = { type: 'object', properties: { name: { type: 'string' }, age: { type: 'integer', minimum: 0 } }, required: ['name'] }; const code = converter.convertSchema(schema, { nsPrefix: 'D.', nsLib: 'L.' }); console.log(code); // Output: object({ name: D.string, age: D.number(D.guard(x => x >= 0)) })
Debug
Known issues
gotchaThe package generates source code strings, not executable decoders. You must have the 'decoders' library installed separately.
fix
Ensure 'decoders' is listed in your dependencies (it is a peer dependency).
affects: *
deprecatedIn versions prior to 0.2.0, the API used a different function signature for convertSchema. Old custom scripts may break.
fix
Upgrade to >=0.2.0 and use the new Converter class with convertSchema(schema, options) signature.
affects: <0.2.0
gotchaThe 'nsLib' option requires importing an additional library file from 'json-schema-to-decoders/decoders'. If omitted, advanced decoders like 'guard', 'any', 'optional' may not work.
fix
Import the lib: import * as L from 'json-schema-to-decoders/decoders'; and pass nsLib: 'L.' in options.
affects: *
gotchaThe package does not validate that the generated code is syntactically correct or that the decoders library functions exist. Use with caution.
fix
After generation, compile the code with TypeScript or run a linter to catch errors.
affects: *
Errors
Common errors & fixes
TypeError: converter.convertSchema is not a function
Wrong import: using named import or destructuring the default export incorrectly
fix
Use default import: import converter from 'json-schema-to-decoders'; then converter.convertSchema(...)
Cannot find module 'decoders'
The 'decoders' peer dependency is not installed
fix
Run: npm install decoders
Error: resolveRefSchema is required (or resolveRefPointer)
The schema contains a $ref but neither resolveRefPointer nor resolveRefSchema option is provided
fix
Add a resolveRefPointer option to convertSchema, e.g., resolveRefPointer: (ref) => ref.split('/').pop()
Upgrade
Version history
0.2.6latest on npm
Audit
Dependencies
decodersoptionalRuntime dependency for the generated decoder code
Agent activity
4 hits · last 30 days
node
4
Resources
json-schema-to-decoders — npm install json-schema-to-decoders · libregistry