npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@inaiat/jsonschema-avro

v1.0.5

Published

Convert JSON Schema into Apache Avro schema

Downloads

69

Readme

json-schema-to-avro

npm Build Status

Converts JSON Schema definitions into Avro Schema definitions.

Install

yarn add @inaiat/jsonschema-avro
yarn add @types/json-schema --dev

Consume

import {convert} from '@inaiat/jsonschema-avro'
import type { JSONSchema7 } from 'json-schema'

const inJson: JSONSchema7 = {
  $id: 'http://your-domain.com/schemas/your-schema.json',
  description: 'Example description',
  type: 'object',
  properties: {
    first_name: { type: 'string' },
    address: {
      type: 'object',
      properties: {
        street_address: { type: 'string' },
      },
    },
  },
}

const avro = convert(inJson)

Cli

  • create a configuration file json-schema-avro.mts with the following content:
import type { Config } from '@inaiat/jsonschema-avro'
import { myDto } from './src/mymodel/myjsonschema.js'

const config: Config = {
  outDir: 'dist/avro-schemas',
  models: {
    user: {
      jsonSchema: myDto,
    },
  },
}

export default config
  • finally add the generate script to your package.json and run it
    {
      // package.json
      "scripts": {
        "generate-avro-models": "jsonschema-avro generate",
        "postbuild": "yarn generate-avro-models" // If you want always generate models at build phase
      }
    }
    
  • Typebox. We can use some library that generates json schema and use directly on jsonSchema field.
// src/user-dto.ts
import type { Static} from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'

export const userDtoSchema = Type.Object({
  id: Type.Optional(Type.String({ maxLength: 22 })),
  name: Type.String({ maxLength: 100, minLength: 10 }),
  phone: Type.String({ maxLength: 20, minLength: 8 }),
}, { $id: 'userDto' })

import type { Config } from '@inaiat/jsonschema-avro'
import { userDtoSchema } from './src/usr-dto.js'

//json-schema-avro.mts
const config: Config = {
  outDir: 'dist/avro-schemas',
  models: {
    user: {
      jsonSchema: userDtoSchema,
    },
  },
}

export default config

Configuration

Json Schema to Avro requires a configuration file written in TypeScript, to ensure the models have applied the decorators accordingly to read the required metadata.

The models record in the config is mandatory. Each entry requires jsonSchema - a reference to the TypeScript json schema (@types/json-schema). The name file is defined by entry name.

Additionally, an output directory outDir can be declared as seen above. If it is not specified, the generated schemas will be generate on folder 'avro-schemas' on root.

Locating config not in root

By default, Json Schema to Avro will check the current working directory for the file json-schema-avro.mts. If your config is located in a different folder, pass it to the program using the flag --config <path> or -c <path>.

Test

npm test

ESM Only

This package only supports to be directly imported in a ESM context.

For informations on how to use it in a CommonJS context, please check this page.

TODO

  • Handle anyOf and allOf.