Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: CI

on:
push:
branches: [main, master]
branches: [master]
tags: ["*"]
pull_request:
release:
Expand Down
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# JSONSchema.jl

[![CI](https://github.com/JuliaServices/JSONSchema.jl/actions/workflows/CI.yml/badge.svg?branch=master)](https://github.com/JuliaServices/JSONSchema.jl/actions?query=workflow%3ACI)
[![codecov](https://codecov.io/gh/JuliaServices/JSONSchema.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaServices/JSONSchema.jl)
[![Docs](https://img.shields.io/badge/docs-stable-blue.svg)](https://juliaservices.github.io/JSONSchema.jl/stable)
[![CI](https://github.com/JuliaIO/JSONSchema.jl/actions/workflows/CI.yml/badge.svg?branch=master)](https://github.com/JuliaIO/JSONSchema.jl/actions?query=workflow%3ACI)
[![codecov](https://codecov.io/gh/JuliaIO/JSONSchema.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaIO/JSONSchema.jl)
[![Docs](https://img.shields.io/badge/docs-stable-blue.svg)](https://juliaio.github.io/JSONSchema.jl/stable)

## Overview

Expand All @@ -11,7 +11,7 @@ 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](https://juliaservices.github.io/JSONSchema.jl/stable/migration/) for breaking changes and upgrade instructions.
> **Upgrading from v1.x?** See the [v2.0 Migration Guide](https://juliaio.github.io/JSONSchema.jl/stable/migration/) for breaking changes and upgrade instructions.

The test harness is wired to the
[JSON Schema Test Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite)
Expand Down Expand Up @@ -39,9 +39,8 @@ end

schema = JSONSchema.schema(User)
user = User(1, "Alice", "alice@example.com")
result = JSONSchema.validate(schema, user)

result.is_valid # true
isvalid(schema, user) # true
```

### Validate JSON data against a schema object
Expand All @@ -58,7 +57,7 @@ schema = JSONSchema.Schema(JSON.parse("""
"""))

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

## Features
Expand All @@ -72,7 +71,7 @@ JSONSchema.isvalid(schema, data) # true

## Documentation

See the [documentation](https://juliaservices.github.io/JSONSchema.jl/stable) for:
See the [documentation](https://juliaio.github.io/JSONSchema.jl/stable) for:
- Complete API reference
- Validation rules and field tags
- Type mapping reference
Expand Down
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ makedocs(
],
)

deploydocs(repo = "github.com/JuliaServices/JSONSchema.jl.git", push_preview = true)
deploydocs(repo = "github.com/JuliaIO/JSONSchema.jl.git", push_preview = true)
103 changes: 49 additions & 54 deletions docs/src/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,80 +10,81 @@ JSONSchema.jl v2.0 introduces:
- **Schema generation** from Julia types via `schema(T)`
- **Type-safe validation** with `Schema{T}`
- **StructUtils integration** for field-level validation rules
- **`\$ref` support** for schema deduplication
- **`$ref` support** for schema deduplication

Most v1.x code will continue to work with minimal changes thanks to our
backwards compatibility layer.

## Breaking Changes

### 1. `validate()` Return Type
### 1. `parent_dir` Keyword Argument Removed

**This is the main breaking change.** The `validate` function now always returns
a `ValidationResult` struct instead of `nothing` on success.
The `Schema` constructor no longer accepts a `parent_dir` keyword argument for
resolving local file `$ref` references.

**v1.x:**
```julia
schema = Schema(spec; parent_dir="./schemas")
```

**v2.0:** Local file reference resolution is not currently supported. External
`$ref` references should be resolved before creating the schema, or use the new
`refs` keyword argument with `schema()` for type-based deduplication.

### 2. `SingleIssue` Type Replaced by `ValidationResult`

The `SingleIssue` type from v1.x has been replaced by `ValidationResult`. For
backwards compatibility, `SingleIssue` is aliased to `ValidationResult`, so
existing `isa` checks will continue to work.

**v1.x:**
```julia
result = validate(schema, data)
if result === nothing
println("Valid!")
else
println(result) # SingleIssue with error details
if result isa SingleIssue
println(result.x) # The invalid value
println(result.path) # JSON path to the error
end
```

**v2.0:**
```julia
result = validate(schema, data)
if result.is_valid
println("Valid!")
else
if result !== nothing
for err in result.errors
println(err)
println(err) # Error message with path
end
end
```

**Migration:** Replace `validate(...) === nothing` with `validate(...).is_valid`
or use `isvalid(schema, data)` which returns a boolean.
## API Compatibility

### 2. `JSON.schema`, `JSON.validate`, `JSON.isvalid` No Longer Available
The following v1.x patterns are fully supported in v2.0:

The v1.x package registered convenience methods on the `JSON` module at runtime.
This is no longer supported.
### `validate()` Return Type (Unchanged)

**v1.x:**
```julia
using JSONSchema
JSON.isvalid(schema, data) # Worked via runtime registration
```
The `validate` function returns `nothing` on success and a `ValidationResult`
on failure, matching v1.x behavior:

**v2.0:**
```julia
using JSONSchema
JSONSchema.isvalid(schema, data) # Use the JSONSchema namespace directly
result = validate(schema, data)
if result === nothing
println("Valid!")
else
for err in result.errors
println(err)
end
end
```

**Migration:** Replace `JSON.schema`, `JSON.validate`, `JSON.isvalid` with
`JSONSchema.schema`, `JSONSchema.validate`, `JSONSchema.isvalid`.
### `isvalid()` Function

### 3. `parent_dir` Keyword Argument Removed
The `isvalid` function extends `Base.isvalid` and returns a boolean:

The `Schema` constructor no longer accepts a `parent_dir` keyword argument for
resolving local file `\$ref` references.

**v1.x:**
```julia
schema = Schema(spec; parent_dir="./schemas")
```

**v2.0:** Local file reference resolution is not currently supported. External
`\$ref` references should be resolved before creating the schema, or use the new
`refs` keyword argument with `schema()` for type-based deduplication.

## Compatibility Layer
using JSONSchema

The following v1.x patterns continue to work in v2.0:
isvalid(schema, data) # Returns true or false
```

### `schema.data` Field Access

Expand Down Expand Up @@ -119,11 +120,7 @@ isvalid(schema, Dict("bar" => 1)) # Returns false (v1.x behavior)
diagnose(data, schema) # Works but emits deprecation warning
```

### `SingleIssue` Type

```julia
result isa SingleIssue # Works - SingleIssue is aliased to ValidationResult
```
Use `validate(schema, data)` instead.

## New Features in v2.0

Expand Down Expand Up @@ -151,12 +148,12 @@ Schemas are now parameterized by the type they describe:
```julia
schema = JSONSchema.schema(User) # Returns Schema{User}
user = User(1, "Alice", "alice@example.com", 30)
JSONSchema.isvalid(schema, user) # Type-safe validation
isvalid(schema, user) # Type-safe validation
```

### `\$ref` Support for Deduplication
### `$ref` Support for Deduplication

Use `refs=true` to generate schemas with `\$ref` for nested types:
Use `refs=true` to generate schemas with `$ref` for nested types:

```julia
@defaults struct Address
Expand All @@ -170,7 +167,7 @@ end
end

schema = JSONSchema.schema(Person, refs=true)
# Generates schema with `\$ref` to #/definitions/Address
# Generates schema with `$ref` to #/definitions/Address
```

### ValidationResult with Error Details
Expand All @@ -179,7 +176,7 @@ Get detailed validation errors:

```julia
result = JSONSchema.validate(schema, invalid_data)
if !result.is_valid
if result !== nothing
for error in result.errors
println(error) # e.g., "name: string length 0 is less than minimum 1"
end
Expand All @@ -188,13 +185,11 @@ end

## Quick Migration Checklist

- [ ] Replace `validate(...) === nothing` with `validate(...).is_valid` or `isvalid(...)`
- [ ] Replace `JSON.schema/validate/isvalid` with `JSONSchema.schema/validate/isvalid`
- [ ] Remove `parent_dir` keyword from `Schema()` calls
- [ ] Update error handling to use `ValidationResult.errors` instead of `SingleIssue` fields
- [ ] Consider using `schema(T)` for type-based schema generation

## Getting Help

If you encounter issues migrating, please [open an issue](https://github.com/JuliaServices/JSONSchema.jl/issues)
If you encounter issues migrating, please [open an issue](https://github.com/JuliaIO/JSONSchema.jl/issues)
with details about your use case.
2 changes: 1 addition & 1 deletion src/JSONSchema.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import StructUtils
import URIs
using JSON: JSONWriteStyle, Object

export Schema, SchemaContext, ValidationResult, schema, validate, isvalid
export Schema, SchemaContext, ValidationResult, schema, validate
# Backwards compatibility exports (v1.5.0)
export diagnose, SingleIssue

Expand Down
7 changes: 4 additions & 3 deletions src/compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ end

# ============= 2. Support inverse argument order =============
# v1.5.0 supported both validate(schema, x) and validate(x, schema)
# NOTE: This is now handled directly in schema.jl via the generic fallback methods
# that check if the second argument is a Schema and swap arguments accordingly.
function validate(instance, schema::Schema; resolver=nothing)
return validate(schema, instance; resolver=resolver)
end

# ============= 3. Support boolean schemas =============
# v1.5.0 supported Schema(true) and Schema(false)
Expand Down Expand Up @@ -75,7 +76,7 @@ function diagnose(x, schema)
:diagnose,
)
result = validate(schema, x)
if !result.is_valid && !isempty(result.errors)
if result !== nothing && !isempty(result.errors)
return join(result.errors, "\n")
end
return nothing
Expand Down
Loading
Loading