Skip to content

JuliaIO/JSONSchema.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

96 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSONSchema.jl

CI codecov Docs

Overview

JSONSchema.jl generates JSON Schema (draft-07) from Julia types and validates instances against those schemas. It also supports validating data against hand-written JSON Schema objects. Field-level validation rules are provided via StructUtils tags.

Upgrading from v1.x? See the v2.0 Migration Guide for breaking changes and upgrade instructions.

The test harness is wired to the JSON Schema Test Suite for draft4, draft6, and draft7.

Installation

using Pkg
Pkg.add("JSONSchema")

Usage

Generate a schema from a Julia type

using JSONSchema, StructUtils

@defaults struct User
    id::Int = 0 &(json=(minimum=1,),)
    name::String = "" &(json=(minLength=1,),)
    email::String = "" &(json=(format="email",),)
end

schema = JSONSchema.schema(User)
user = User(1, "Alice", "[email protected]")

isvalid(schema, user)  # true

Validate JSON data against a schema object

using JSON, JSONSchema

schema = JSONSchema.Schema(JSON.parse("""
{
  "type": "object",
  "properties": {"foo": {"type": "integer"}},
  "required": ["foo"]
}
"""))

data = JSON.parse("""{"foo": 1}""")
isvalid(schema, data)  # true

Features

  • Schema Generation: Automatically generate JSON Schema from Julia struct definitions
  • Type-Safe Validation: Validate Julia instances against generated schemas
  • StructUtils Integration: Use field tags for validation rules (min/max, patterns, formats, etc.)
  • Composition Support: oneOf, anyOf, allOf, not combinators
  • Reference Support: $ref with definitions for complex/recursive types
  • Format Validation: Built-in validators for email, uri, uuid, date-time

Documentation

See the documentation for:

  • Complete API reference
  • Validation rules and field tags
  • Type mapping reference
  • Advanced usage with $ref and composition

About

JSON Schema validation package for Julia

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 14

Languages