Sign inSign up

craftmachine/ajv-tools

By craftmachine

Updated over 3 years ago

AJV and other related tools to unify working with JSON Schemas across different tech stacks

Image
0

610

craftmachine/ajv-tools repository overview

AJV Tools

Docker image with cross-stack tooling to work with AJV - JSON Schema validation tool.

Build

# Build new version for local use
make build VERSION=0.1.6

# Build/push
make publish VERSION=0.1.6

Usage

Example

Granted a project with /src/schemas folder containing all required JSON Schema definitions:

/src
   /schemas
      config.schema.jsonc
      payload.schema.yml

Add the craftmachine/ajv-tools image to a task orchestrator used by the project. In case of Node.js, that could be scripts in package.json:

{
   "scripts": {
      // An alias for incapsulating common parameters for the tool.
      // We're mounting "/src/schemas" as "/schemas" in a container.
      // "DOCKER_USER" variable is recommended to help the tool to write
      // files with the same access as the host User you're running
      // the container with, so that there are no files in a project written
      // by "root" user (docker's default)
      "dev:ajv": "DOCKER_USER=\"$(id -u):$(id -g)\"; docker run --user=$DOCKER_USER -v ./src/schemas:/schemas craftmachine/ajv-tools:0.1.6",

      // This command will produce "/src/schemas/generated/config.schema.sql"
      // that will contain the code you can use in a migration file.
      "schema:compile:config": "yarn dev:ajv compile -s /schemas/config.schema.jsonc",
      // Symmetrical to the command above, but for "payload.schema.yml"
      "schema:compile:payload": "yarn dev:ajv compile -s /schemas/payload.schema.jsonc",
      // Orchestrate compilation into a single command
      "schema:compile": "yarn schema:compile:config && schema:compile:payload",
      // This will compile config, but with a different output directory
      // In this case "/src/generatedOutput/config.schema.sql"
      "schema:compile:config": "yarn dev:ajv compile -s /schemas/config.schema.jsonc -o ../generatedOutput",

      // This command will create "/src/schemas/docs" folder that will
      // contain markdown documentation for all the schemas in the "/src/schemas"
      "schema:docs": "yarn dev:ajv docs -f /schemas/",
      // Same as above, but with a different output directory
      // In this case "/src/docs"
      "schema:docs": "yarn dev:ajv docs -f /schemas/ -o ../docs",

      // This command will create "/src/schemas/generatedSchemas/sample.json" file
      // that is a generated JSON schema definition from the input json file
      "schema:generateSchema": "yarn dev:ajv generateSchema -f /schemas/sample.json",
      // Same as above, but with a different output directory and different extension
      // In this case "/src/generatedSchemas/sample.jsonc" with extension "jsonc"
      "schema:generateSchema": "yarn dev:ajv generateSchema -f /schemas/sample.json -o ../generatedSchemas -e jsonc",

      // Optionally, orchestrate the whole workflow as a single command
      "dev:schemas": "yarn schema:compile && yarn schema:docs",
   }
}

Dockerfile

# Contains AJV + Node.js/Python for convenience
FROM craftmachine/ajv-tools:0.1.6
# ...

CLI

Four main commands exposed by this image are:

  • compile - converts a given JSON Schema definition into RDS-compatible SQL
  • docs - compiles Markdown Docs for all JSON Schemas in a given directory
  • ajv - allows using AJV's CLI directly when you need that
  • generateSchema - generates a JSON schema definition from a JSON file of an array of sample payloads

See the usage examples below:

function ajv_tools() {
   # See the explanation for "--user" arg in the comment for "DOCKER_USER" variable above.
    docker run --user="$(id -u):$(id -g)" -v ./schemas:/schemas craftmachine/ajv-tools:0.1.6 "$@"
}

# Compile a JSON Schema to an RDS-compatible SQL for a migration
# Will produce a risk_config.sql in the "./schemas" that you can copy over to a migration file.
# ./schemas/risk_config.json -> ./schemas/generated/risk_config.sql
$ ajv_tools compile -s /schemas/risk_config.json

# You can also compile JSONC/JSON5/YAML schemas in exactly the same fashion
# ./schemas/risk_config.yml -> ./schemas/generated/risk_config.sql
$ ajv_tools compile -s /schemas/risk_config.yml

# Will produce markdown documentation for all the JSON
# schema definitions in the "schemas" folder.
# The documentation will be placed in the "docs" folder.
$ ajv_tools docs -f /schemas

# You can use AJV's CLI directly without the wrapper, you'll need to convert
# compiled code to required format manually.
# ./schemas/risk_config.json -> ./schemas/risk_config.js
$ ajv_tools ajv compile -s /schemas/risk_config.json -o /schemas/risk_config.js

# Will print all available commands for the image
# It has a few other goodies that might be useful in case you need them.
ajv_tools
info Commands available from binary scripts: ajv, esparse, esvalidate, js-yaml, json5
info Project commands
   - ajv
      ajv
   - compile
      ajv compile
error No command specified.
Done in 0.02s.

Changelog

0.1.6

  • fix: removing schema definitions from being rejected by compile command

0.1.5

  • feature: ability to pick output directory to all functions via '-o'
  • feature: generateSchema ability to set output extension 'json', 'jsonc' or 'json5'
  • fix: compile fixed issue with const required by validator function

0.1.4

  • feature: generateSchema command to generate single schema based on a JSON file of an array of sample payloads

0.1.3

  • fix: docs - cleanup docs folder before generation to avoid leaving orphaned .md files when changing titles of the entities in the schema
  • fix: compile - adding all of the exports from compiled validator to the generated SQL to support patterns/refs

0.1.2

  • fix: compile to produce SQL file with EOF newline to make GitHub happy
  • fix: docs - removing unwanted logs
  • docs: adding a more detailed example on intended usage workflow

0.1.1

  • feature: docs command to generate markdown documentation for schemas in a folder

0.0.1

  • feature: compile command to compile JSON Schema into RDS-compatible SQL

Tag summary

Content type

Image

Digest

sha256:1b60a63a9

Size

137.7 MB

Last updated

over 3 years ago

docker pull craftmachine/ajv-tools:0.1.6