qri-io / Jsonschema
Licence: mit
golang implementation of https://json-schema.org drafts 7 & 2019-09
Stars: β 267
Labels
Projects that are alternatives of or similar to Jsonschema
commun
π© Fully-featured framework for REST APIs and GraphQL from JSON Schema with TypeScript and MongoDB
Stars: β 97 (-63.67%)
Mutual labels: json-schema
openapi-schemas
JSON Schemas for every version of the OpenAPI Specification
Stars: β 22 (-91.76%)
Mutual labels: json-schema
json-ref-resolver
[Deprecated] Recursively resolve JSON pointers and remote authorities.
Stars: β 27 (-89.89%)
Mutual labels: json-schema
i18n-tag-schema
Generates a json schema for all i18n tagged template literals in your project
Stars: β 15 (-94.38%)
Mutual labels: json-schema
json-schema-inferrer
Java library for inferring JSON schema from sample JSONs
Stars: β 78 (-70.79%)
Mutual labels: json-schema
home
This is the home page for the API specification toolbox.
Stars: β 16 (-94.01%)
Mutual labels: json-schema
Json Schema To Ts
Infer TS types from JSON schemas π
Stars: β 261 (-2.25%)
Mutual labels: json-schema
grunt-tv4
Use Grunt and Tiny Validator tv4 to validate files against json-schema draft v4
Stars: β 13 (-95.13%)
Mutual labels: json-schema
fform
Flexibile and extendable form builder with constructor
Stars: β 26 (-90.26%)
Mutual labels: json-schema
kloadgen
KLoadGen is kafka load generator plugin for jmeter designed to work with AVRO and JSON schema Registries.
Stars: β 144 (-46.07%)
Mutual labels: json-schema
modelina
Library for generating data models based on inputs such as AsyncAPI, OpenAPI, or JSON Schema documents.
Stars: β 55 (-79.4%)
Mutual labels: json-schema
form-pa
A flexible and configurable form based on json schema
Stars: β 13 (-95.13%)
Mutual labels: json-schema
caddy-json-schema
JSON schema generator for Caddy v2
Stars: β 63 (-76.4%)
Mutual labels: json-schema
Jsonschema
JSONSchema (draft04, draft06, draft07) Validation using Go
Stars: β 261 (-2.25%)
Mutual labels: json-schema
php-code-builder
JSON Schema enabled PHP code building abstraction for PHP
Stars: β 55 (-79.4%)
Mutual labels: json-schema
jsonschema
golang implementation of the JSON Schema Specification, which lets you write JSON that validates some other json. Rad.
Package Features
- Encode schemas back to JSON
- Supply Your own Custom Validators
- Uses Standard Go idioms
- Fastest Go implementation of JSON Schema validators (draft2019_9 only, (old β draft 7) benchmarks are here β thanks @TheWildBlue!)
Getting Involved
We would love involvement from more people! If you notice any errors or would like to submit changes, please see our Contributing Guidelines.
Developing
Weβve set up a separate document for developer guidelines!
Basic Usage
Hereβs a quick example pulled from the godoc:
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/qri-io/jsonschema"
)
func main() {
ctx := context.Background()
var schemaData = []byte(`{
"$id": "https://qri.io/schema/",
"$comment" : "sample comment",
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
},
"friends": {
"type" : "array",
"items" : { "title" : "REFERENCE", "$ref" : "#" }
}
},
"required": ["firstName", "lastName"]
}`)
rs := &jsonschema.Schema{}
if err := json.Unmarshal(schemaData, rs); err != nil {
panic("unmarshal schema: " + err.Error())
}
var valid = []byte(`{
"firstName" : "George",
"lastName" : "Michael"
}`)
errs, err := rs.ValidateBytes(ctx, valid)
if err != nil {
panic(err)
}
if len(errs) > 0 {
fmt.Println(errs[0].Error())
}
var invalidPerson = []byte(`{
"firstName" : "Prince"
}`)
errs, err = rs.ValidateBytes(ctx, invalidPerson)
if err != nil {
panic(err)
}
if len(errs) > 0 {
fmt.Println(errs[0].Error())
}
var invalidFriend = []byte(`{
"firstName" : "Jay",
"lastName" : "Z",
"friends" : [{
"firstName" : "Nas"
}]
}`)
errs, err = rs.ValidateBytes(ctx, invalidFriend)
if err != nil {
panic(err)
}
if len(errs) > 0 {
fmt.Println(errs[0].Error())
}
}
// Output:
// /: {"firstName":"Prince... "lastName" value is required
// /friends/0: {"firstName":"Nas"} "lastName" value is required
Custom Keywords
The godoc gives an example of how to supply your own validators to extend the standard keywords supported by the spec.
It involves three steps that should happen before allocating any Schema instances that use the validator:
- create a custom type that implements the
Keyword
interface - Load the appropriate draft keyword set (see
draft2019_09_keywords.go
) - call RegisterKeyword with the keyword youβd like to detect in JSON, and a
KeyMaker
function.
package main
import (
"context"
"encoding/json"
"fmt"
jptr "github.com/qri-io/jsonpointer"
"github.com/qri-io/jsonschema"
)
// your custom validator
type IsFoo bool
// newIsFoo is a jsonschama.KeyMaker
func newIsFoo() jsonschema.Keyword {
return new(IsFoo)
}
// Validate implements jsonschema.Keyword
func (f *IsFoo) Validate(propPath string, data interface{}, errs *[]jsonschema.KeyError) {}
// Register implements jsonschema.Keyword
func (f *IsFoo) Register(uri string, registry *jsonschema.SchemaRegistry) {}
// Resolve implements jsonschema.Keyword
func (f *IsFoo) Resolve(pointer jptr.Pointer, uri string) *jsonschema.Schema {
return nil
}
// ValidateKeyword implements jsonschema.Keyword
func (f *IsFoo) ValidateKeyword(ctx context.Context, currentState *jsonschema.ValidationState, data interface{}) {
if str, ok := data.(string); ok {
if str != "foo" {
currentState.AddError(data, fmt.Sprintf("should be foo. plz make '%s' == foo. plz", str))
}
}
}
func main() {
// register a custom validator by supplying a function
// that creates new instances of your Validator.
jsonschema.RegisterKeyword("foo", newIsFoo)
// If you register a custom validator, you'll need to manually register
// any other JSON Schema validators you need.
jsonschema.LoadDraft2019_09()
schBytes := []byte(`{ "foo": true }`)
rs := new(jsonschema.Schema)
if err := json.Unmarshal(schBytes, rs); err != nil {
// Real programs handle errors.
panic(err)
}
errs, err := rs.ValidateBytes(context.Background(), []byte(`"bar"`))
if err != nil {
panic(err)
}
fmt.Println(errs[0].Error())
// Output: /: "bar" should be foo. plz make 'bar' == foo. plz
}
Note that the project description data, including the texts, logos, images, and/or trademarks,
for each open source project belongs to its rightful owner.
If you wish to add or remove any projects, please contact us at [email protected].