🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@janus-validator/avro

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@janus-validator/avro

Avro schema import/export for Janus Validator

latest
npmnpm
Version
0.6.0
Version published
Weekly downloads
15
114.29%
Maintainers
1
Weekly downloads
 
Created
Source

@janus-validator/avro

Avro schema import/export for Janus Validator.

Installation

npm install @janus-validator/avro @janus-validator/core

Usage

🚧 Status: Avro conversion is scaffolded but not yet implemented. The exported functions currently throw Error('Not yet implemented').

Why this package exists

Avro schemas describe shape but don’t standardize validation constraints (lengths, numeric bounds, regex patterns, etc). This package is intended to bridge that gap by encoding validation constraints as x-janus-* extension fields, so the same schema can be:

  • Validated forwards (Avro → Janus validator → validate)
  • Generated backwards (Janus validator → Generator → fixtures)

Import: Avro → Validator

Convert an Avro schema to a Janus validator:

import { avroToValidator } from '@janus-validator/avro';

const avroSchema = {
  type: 'record',
  name: 'User',
  fields: [
    {
      name: 'name',
      type: 'string',
      'x-janus-minLength': 1,
      'x-janus-maxLength': 100
    },
    {
      name: 'age',
      type: 'int',
      'x-janus-min': 0,
      'x-janus-max': 150
    },
    {
      name: 'email',
      type: 'string',
      'x-janus-pattern': '^[\\w.]+@[\\w.]+\\.\\w+$'
    }
  ]
};

const validator = avroToValidator(avroSchema);

// NOTE: Currently throws "Not yet implemented"

// And generate test data
import { Generator } from '@janus-validator/core';
const generator = new Generator({ random: Math.random });
const testUser = generator.generate(validator);

Export: Validator → Avro

Convert a Janus validator to an Avro schema:

import { validatorToAvro } from '@janus-validator/avro';
import { O, U, I, R } from '@janus-validator/dsl';

const userValidator = O({
  name: U(1, 100),
  age: I(0, 150),
  email: R(/^[\w.]+@[\w.]+\.\w+$/),
});

const schema = validatorToAvro(userValidator, {
  name: 'User',
  namespace: 'com.example',
  includeExtensions: true
});

console.log(JSON.stringify(schema, null, 2));
// {
//   "type": "record",
//   "name": "User",
//   "namespace": "com.example",
//   "fields": [
//     { "name": "name", "type": "string", "x-janus-minLength": 1, "x-janus-maxLength": 100 },
//     { "name": "age", "type": "int", "x-janus-min": 0, "x-janus-max": 150 },
//     { "name": "email", "type": "string", "x-janus-pattern": "^[\\w.]+@[\\w.]+\\.\\w+$" }
//   ]
// }

Extended Schema Fields

Since Avro doesn't natively support validation constraints, this package uses x-janus-* prefixed fields:

FieldTypeDescription
x-janus-minnumberMinimum value for numeric types
x-janus-maxnumberMaximum value for numeric types
x-janus-minLengthnumberMinimum length for string/bytes
x-janus-maxLengthnumberMaximum length for string/bytes
x-janus-patternstringRegex pattern for strings
x-janus-enumunknown[]Allowed constant values
x-janus-minItemsnumberMinimum array length
x-janus-maxItemsnumberMaximum array length
x-janus-descriptionstringHuman-readable description
x-janus-examplesunknown[]Example values for generation

Type Mapping

Avro TypeJanus Validator
nullNull()
booleanB()
intI(min?, max?)
longL(min?, max?)
floatN(min?, max?)
doubleN(min?, max?)
stringU(minLen?, maxLen?) or R(pattern)
bytesBytes(minLen?, maxLen?)
arrayoneOrMore(item) / between(item, min, max)
recordO({ ... })
enumOr('a', 'b', ...)
unionOr(type1, type2, ...)

Roadmap

Planned behavior once implemented:

  • avroToValidator(schema):
    • Produces a validator from Avro schema + x-janus-* constraints
    • Records become Struct(...) / DSL O(...) with configurable strictness
  • validatorToAvro(validator):
    • Encodes domains/constraints back into Avro types + x-janus-* fields

License

MIT

Keywords

avro

FAQs

Package last updated on 12 Dec 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts