Skip to content

ryanolee/go-chaff

Repository files navigation

go-chaff

A Json Schema Faker for Go 🙈.

It will generate matching random for a given JSON schema

[CC0 by @Iroshi_]

Documentation

Documentation for the library functions can be found here.

Installation

go get github.com/ryanolee/go-chaff@1

Usage

package main

import (
	"encoding/json"
	"fmt"

	"github.com/ryanolee/go-chaff"
)

const schema = `{"type": "number"}`

func main() {
	generator, err := chaff.ParseSchemaStringWithDefaults(schema)
	if err != nil {
		panic(err)
	}
	
	fmt.Println(generator.Metadata.Errors)
	result := generator.GenerateWithDefaults()
	if err != nil {
		panic(err)
	}

	res, err := json.Marshal(result)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(res))
}

CLI

There is also a cli tool available for this package that can be installed from the releases section.

Usage: go-chaff [flags]
  -file string
        Specify a file path to read the JSON Schema from
  -format
        Format JSON output.
  -help
        Print out help.
  -output string
        Specify file path to write generated output to.
  -verbose
        Print out detailed error information.
  -version
        Print out cli version information.

You can also pipe into STDIN for the cli

echo '{"type": "string", "format": "ipv4"}' | go-chaff
"217.2.244.95"

Current support:

  • Strings: (Including pattern through regen and formats through go faker)
  • Number / Integer: multipleOf, min, max, exclusiveMin, exclusiveMax
  • Constant types: enum, const, null
  • References: $ref, $defs, definitions, $id
  • Object: properties, patternProperties, additionalProperties, minProperties, maxProperties, required
  • Array: items, minItems, maxItems, contains, minContains, maxContains, prefixItems, additionalItems
  • Combination types (anyOf / oneOf / allOf) N.b These are experimental! Expect none compliant schema output for some of these

Credits / Dependencies

What is left to do?

  • Better test coverage (Property based and unit of various generators)
  • Handle many edge cases where this package might not generate schema compliant results
  • Overcome the limitations of the current oneOf, anyOf and allOf keywords implementation.
  • Add support for if / else keywords