jspg progress
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
/target
|
||||
/package
|
||||
.env
|
||||
.env
|
||||
/src/tests.rs
|
||||
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +1,6 @@
|
||||
[submodule "flows"]
|
||||
path = flows
|
||||
url = git@gitea-ssh.thoughtpatterns.ai:cellular/flows.git
|
||||
[submodule "tests/fixtures/JSON-Schema-Test-Suite"]
|
||||
path = tests/fixtures/JSON-Schema-Test-Suite
|
||||
url = git@github.com:json-schema-org/JSON-Schema-Test-Suite.git
|
||||
|
||||
1512
Cargo.lock
generated
1512
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
32
Cargo.toml
32
Cargo.toml
@ -1,9 +1,3 @@
|
||||
[workspace]
|
||||
members = [
|
||||
".",
|
||||
"validator",
|
||||
]
|
||||
|
||||
[package]
|
||||
name = "jspg"
|
||||
version = "0.1.0"
|
||||
@ -11,14 +5,28 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
pgrx = "0.16.1"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
serde = { version = "1.0.228", features = ["derive", "rc"] }
|
||||
serde_json = "1.0.149"
|
||||
lazy_static = "1.5.0"
|
||||
boon = { path = "validator" }
|
||||
once_cell = "1.21.3"
|
||||
ahash = "0.8.12"
|
||||
regex = "1.12.3"
|
||||
regex-syntax = "0.8.9"
|
||||
url = "2.5.8"
|
||||
fluent-uri = "0.3.2"
|
||||
idna = "1.1.0"
|
||||
percent-encoding = "2.3.2"
|
||||
uuid = { version = "1.20.0", features = ["v4", "serde"] }
|
||||
chrono = { version = "0.4.43", features = ["serde"] }
|
||||
json-pointer = "0.3.4"
|
||||
|
||||
[dev-dependencies]
|
||||
pgrx-tests = "0.16.1"
|
||||
|
||||
[build-dependencies]
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.149"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "lib"]
|
||||
|
||||
@ -27,6 +35,7 @@ name = "pgrx_embed_jspg"
|
||||
path = "src/bin/pgrx_embed.rs"
|
||||
|
||||
[features]
|
||||
default = ["pg18"]
|
||||
pg18 = ["pgrx/pg18", "pgrx-tests/pg18" ]
|
||||
# Local feature flag used by `cargo pgrx test`
|
||||
pg_test = []
|
||||
@ -39,4 +48,7 @@ lto = "thin"
|
||||
panic = "unwind"
|
||||
opt-level = 3
|
||||
lto = "fat"
|
||||
codegen-units = 1
|
||||
codegen-units = 1
|
||||
|
||||
[package.metadata.jspg]
|
||||
target_draft = "draft2020-12"
|
||||
164
GEMINI.md
164
GEMINI.md
@ -1,135 +1,99 @@
|
||||
# Gemini Project Overview: `jspg`
|
||||
# JSPG: JSON Schema Postgres
|
||||
|
||||
This document outlines the purpose of the `jspg` project, its architecture, and the specific modifications made to the vendored `boon` JSON schema validator crate.
|
||||
**JSPG** is a high-performance PostgreSQL extension for in-memory JSON Schema validation, specifically targeting **Draft 2020-12**.
|
||||
|
||||
## What is `jspg`?
|
||||
It is designed to serve as the validation engine for the "Punc" architecture, where the database is the single source of truth for all data models and API contracts.
|
||||
|
||||
`jspg` is a PostgreSQL extension written in Rust using the `pgrx` framework. Its primary function is to provide fast, in-database JSON schema validation against the 2020-12 draft of the JSON Schema specification.
|
||||
## 🎯 Goals
|
||||
|
||||
### How It Works
|
||||
1. **Draft 2020-12 Compliance**: Attempt to adhere to the official JSON Schema Draft 2020-12 specification.
|
||||
2. **Ultra-Fast Validation**: Compile schemas into an optimized in-memory representation for near-instant validation during high-throughput workloads.
|
||||
3. **Connection-Bound Caching**: Leverage the PostgreSQL session lifecycle to maintain a per-connection schema cache, eliminating the need for repetitive parsing.
|
||||
4. **Structural Inheritance**: Support object-oriented schema design via Implicit Keyword Shadowing and virtual `.family` schemas.
|
||||
5. **Punc Integration**: validation is aware of the "Punc" context (request/response) and can validate `cue` objects efficiently.
|
||||
|
||||
The extension is designed for high-performance scenarios where schemas are defined once and used many times for validation. It achieves this through an in-memory cache.
|
||||
## 🔌 API Reference
|
||||
|
||||
1. **Caching and Pre-processing:** A user first calls the `cache_json_schemas(enums, types, puncs)` SQL function. This function takes arrays of JSON objects representing different kinds of schemas:
|
||||
- `enums`: Standalone enum schemas (e.g., for a `task_priority` list).
|
||||
- `types`: Schemas for core application data models (e.g., `person`, `organization`). These may contain a `hierarchy` array for inheritance information.
|
||||
- `puncs`: Schemas for API/function-specific requests and responses.
|
||||
The extension exposes the following functions to PostgreSQL:
|
||||
|
||||
Before compiling, `jspg` performs a crucial **pre-processing step** for type hierarchies. It inspects each definition in the `types` array. If a type includes a `hierarchy` array (e.g., a `person` type with `["entity", "organization", "user", "person"]`), `jspg` uses this to build a map of "type families."
|
||||
### `cache_json_schemas(enums jsonb, types jsonb, puncs jsonb) -> jsonb`
|
||||
|
||||
From this map, it generates new, virtual schemas on the fly. For example, for the `organization` type, it will generate a schema with `$id: "organization.family"` that contains an `enum` of all its descendant types, such as `["organization", "user", 'person"]`.
|
||||
Loads and compiles the entire schema registry into the session's memory.
|
||||
|
||||
This allows developers to write more flexible schemas. Instead of strictly requiring a `const` type, you can validate against an entire inheritance chain:
|
||||
* **Inputs**:
|
||||
* `enums`: Array of enum definitions.
|
||||
* `types`: Array of type definitions (core entities).
|
||||
* `puncs`: Array of punc (function) definitions with request/response schemas.
|
||||
* **Behavior**:
|
||||
* Parses all inputs into an internal schema graph.
|
||||
* Resolves all internal references (`$ref`).
|
||||
* Generates virtual `.family` schemas for type hierarchies.
|
||||
* Compiles schemas into validators.
|
||||
* **Returns**: `{"response": "success"}` or an error object.
|
||||
|
||||
```json
|
||||
// In an "organization" schema definition
|
||||
"properties": {
|
||||
"type": {
|
||||
// Allows the 'type' field to be "organization", "user", or "person"
|
||||
"$ref": "organization.family",
|
||||
"override": true
|
||||
}
|
||||
}
|
||||
```
|
||||
### `validate_json_schema(schema_id text, instance jsonb) -> jsonb`
|
||||
|
||||
Finally, all user-defined schemas and the newly generated `.family` schemas are passed to the vendored `boon` crate, compiled into an efficient internal format, and stored in a static, in-memory `SCHEMA_CACHE`. This cache is managed by a `RwLock` to allow for high-performance, concurrent reads during validation.
|
||||
Validates a JSON instance against a pre-compiled schema.
|
||||
|
||||
2. **Validation:** The `validate_json_schema(schema_id, instance)` SQL function is then used to validate a JSONB `instance` against a specific, pre-cached schema identified by its `$id`. This function looks up the compiled schema in the cache and runs the validation, returning a success response or a detailed error report.
|
||||
* **Inputs**:
|
||||
* `schema_id`: The `$id` of the schema to validate against (e.g., `person`, `save_person.request`).
|
||||
* `instance`: The JSON data to validate.
|
||||
* **Returns**:
|
||||
* On success: `{"response": "success"}`
|
||||
* On failure: A JSON object containing structured errors (e.g., `{"errors": [...]}`).
|
||||
|
||||
3. **Custom Logic:** `jspg` uses a locally modified (vendored) version of the `boon` crate. This allows for powerful, application-specific validation logic that goes beyond the standard JSON Schema specification, such as runtime-based strictness.
|
||||
### `json_schema_cached(schema_id text) -> bool`
|
||||
|
||||
### Error Handling
|
||||
Checks if a specific schema ID is currently present in the cache.
|
||||
|
||||
When validation fails, `jspg` provides a detailed error report in a consistent JSON format, which we refer to as a "DropError". This process involves two main helper functions in `src/lib.rs`:
|
||||
### `clear_json_schemas() -> jsonb`
|
||||
|
||||
1. **`collect_errors`**: `boon` returns a nested tree of `ValidationError` objects. This function recursively traverses that tree to find the most specific, underlying causes of the failure. It filters out structural errors (like `allOf` or `anyOf`) to create a flat list of concrete validation failures.
|
||||
Clears the current session's schema cache, freeing memory.
|
||||
|
||||
2. **`format_errors`**: This function takes the flat list of errors and transforms each one into the final DropError JSON format. It also de-duplicates errors that occur at the same JSON Pointer path, ensuring a cleaner output if a single value violates multiple constraints.
|
||||
### `show_json_schemas() -> jsonb`
|
||||
|
||||
#### DropError Format
|
||||
Returns a debug dump of the currently cached schemas (for development/debugging).
|
||||
|
||||
A DropError object provides a clear, structured explanation of a validation failure:
|
||||
## ✨ Custom Features & Deviations
|
||||
|
||||
```json
|
||||
{
|
||||
"code": "ADDITIONAL_PROPERTIES_NOT_ALLOWED",
|
||||
"message": "Property 'extra' is not allowed",
|
||||
"details": {
|
||||
"path": "/extra",
|
||||
"context": "not allowed",
|
||||
"cause": {
|
||||
"got": [
|
||||
"extra"
|
||||
]
|
||||
},
|
||||
"schema": "basic_strict_test.request"
|
||||
}
|
||||
}
|
||||
```
|
||||
JSPG implements specific extensions to the Draft 2020-12 standard to support the Punc architecture's object-oriented needs.
|
||||
|
||||
- `code` (string): A machine-readable error code (e.g., `ADDITIONAL_PROPERTIES_NOT_ALLOWED`, `MIN_LENGTH_VIOLATED`).
|
||||
- `message` (string): A human-readable summary of the error.
|
||||
- `details` (object):
|
||||
- `path` (string): The JSON Pointer path to the invalid data within the instance.
|
||||
- `context` (any): The actual value that failed validation.
|
||||
- `cause` (any): The low-level reason from the validator, often including the expected value (`want`) and the actual value (`got`).
|
||||
- `schema` (string): The `$id` of the schema that was being validated.
|
||||
### 1. Implicit Keyword Shadowing
|
||||
Standard JSON Schema composition (`allOf`) is additive (Intersection), meaning constraints can only be tightened, not replaced. However, JSPG treats `$ref` differently when it appears alongside other properties to support object-oriented inheritance.
|
||||
|
||||
---
|
||||
* **Inheritance (`$ref` + `properties`)**: When a schema uses `$ref` *and* defines its own properties, JSPG implements **Smart Merge** (or Shadowing). If a property is defined in the current schema, its constraints take precedence over the inherited constraints for that specific keyword.
|
||||
* *Example*: If `Entity` defines `type: { const: "entity" }` and `Person` (which refs Entity) defines `type: { const: "person" }`, validation passes for "person". The local `const` shadows the inherited `const`.
|
||||
* *Granularity*: Shadowing is per-keyword. If `Entity` defined `type: { const: "entity", minLength: 5 }`, `Person` would shadow `const` but still inherit `minLength: 5`.
|
||||
|
||||
## `boon` Crate Modifications
|
||||
* **Composition (`allOf`)**: When using `allOf`, standard intersection rules apply. No shadowing occurs; all constraints from all branches must pass. This is used for mixins or interfaces.
|
||||
|
||||
The version of `boon` located in the `validator/` directory has been significantly modified to support application-specific validation logic that goes beyond the standard JSON Schema specification.
|
||||
### 2. Virtual Family Schemas (`.family`)
|
||||
To support polymorphic fields (e.g., a field that accepts any "User" type), JSPG generates virtual schemas representing type hierarchies.
|
||||
|
||||
### 1. Property-Level Overrides for Inheritance
|
||||
* **Mechanism**: When caching types, if a type defines a `hierarchy` (e.g., `["entity", "organization", "person"]`), JSPG generates a schema like `organization.family` which is a `oneOf` containing refs to all valid descendants.
|
||||
|
||||
- **Problem:** A primary use case for this project is validating data models that use `$ref` to create inheritance chains (e.g., a `person` schema `$ref`s a `user` schema, which `$ref`s an `entity` schema). A common pattern is to use a `const` keyword on a `type` property to identify the specific model (e.g., `"type": {"const": "person"}`). However, standard JSON Schema composition with `allOf` (which is implicitly used by `$ref`) treats these as a logical AND. This creates an impossible condition where an instance's `type` property would need to be "person" AND "user" AND "entity" simultaneously.
|
||||
### 3. Strict by Default & Extensibility
|
||||
JSPG enforces a "Secure by Default" philosophy. All schemas are treated as if `unevaluatedProperties: false` (and `unevaluatedItems: false`) is set, unless explicitly overridden.
|
||||
|
||||
- **Solution:** We've implemented a custom, explicit override mechanism. A new keyword, `"override": true`, can be added to any property definition within a schema.
|
||||
* **Strictness**: By default, any property in the instance data that is not explicitly defined in the schema causes a validation error. This prevents clients from sending undeclared fields.
|
||||
* **Extensibility (`extensible: true`)**: To allow additional, undefined properties, you must add `"extensible": true` to the schema. This is useful for types that are designed to be open for extension.
|
||||
* **Ref Boundaries**: Strictness is reset when crossing `$ref` boundaries. The referenced schema's strictness is determined by its own definition (strict by default unless `extensible: true`), ignoring the caller's state.
|
||||
* **Inheritance**: Strictness is inherited. A schema extending a strict parent will also be strict unless it declares itself `extensible: true`. Conversely, a schema extending a loose parent will also be loose unless it declares itself `extensible: false`.
|
||||
|
||||
```json
|
||||
// person.json
|
||||
{
|
||||
"$id": "person",
|
||||
"$ref": "user",
|
||||
"properties": {
|
||||
"type": { "const": "person", "override": true }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This signals to the validator that this definition of the `type` property should be the *only* one applied, and any definitions for `type` found in base schemas (like `user` or `entity`) should be ignored for the duration of this validation.
|
||||
### 4. Format Leniency for Empty Strings
|
||||
To simplify frontend form logic, the format validators for `uuid`, `date-time`, and `email` explicitly allow empty strings (`""`). This treats an empty string as "present but unset" rather than "invalid format".
|
||||
|
||||
#### Key Changes
|
||||
## 🏗️ Architecture
|
||||
|
||||
This was achieved by making the validator stateful, using a pattern already present in `boon` for handling `unevaluatedProperties`.
|
||||
The extension is written in Rust using `pgrx` and structures its schema parser to mirror the Punc Generator's design:
|
||||
|
||||
1. **Meta-Schema Update**: The meta-schema for Draft 2020-12 was modified to recognize `"override": true` as a valid keyword within a schema object, preventing the compiler from rejecting our custom schemas.
|
||||
* **Single `Schema` Struct**: A unified struct representing the exact layout of a JSON Schema object, including standard keywords and custom vocabularies (`form`, `display`, etc.).
|
||||
* **Compiler Phase**: schema JSONs are parsed into this struct, linked (references resolved), and then compiled into an efficient validation tree.
|
||||
* **Validation Phase**: The compiled validators traverse the JSON instance using `serde_json::Value`.
|
||||
|
||||
2. **Compiler Modification**: The schema compiler in `validator/src/compiler.rs` was updated. It now inspects sub-schemas within a `properties` keyword and, if it finds `"override": true`, it records the name of that property in a new `override_properties` `HashSet` on the compiled `Schema` struct.
|
||||
## 🧪 Testing
|
||||
|
||||
3. **Stateful Validator with `Override` Context**: The core `Validator` in `validator/src/validator.rs` was modified to carry an `Override` context (a `HashSet` of property names) throughout the validation process.
|
||||
- **Initialization**: When validation begins, the `Override` context is created and populated with the names of any properties that the top-level schema has marked with `override`.
|
||||
- **Propagation**: As the validator descends through a `$ref` or `allOf`, this `Override` context is cloned and passed down. The child schema adds its own override properties to the set, ensuring that higher-level overrides are always maintained.
|
||||
- **Enforcement**: In `obj_validate`, before a property is validated, the validator first checks if the property's name exists in the `Override` context it has received. If it does, it means a parent schema has already claimed responsibility for validating this property, so the child validator **skips** it entirely. This effectively achieves the "top-level wins" inheritance model.
|
||||
Testing is driven by standard Rust unit tests that load JSON fixtures.
|
||||
|
||||
This approach cleanly integrates our desired inheritance behavior directly into the validator with minimal and explicit deviation from the standard, avoiding the need for a complex, post-processing validation function like the old `walk_and_validate_refs`.
|
||||
|
||||
### 2. Recursive Runtime Strictness Control
|
||||
|
||||
- **Problem:** The `jspg` project requires that certain schemas (specifically those for public `puncs` and global `type`s) enforce a strict "no extra properties" policy. This strictness needs to be decided at runtime and must cascade through the entire validation hierarchy, including all nested objects and `$ref` chains. A compile-time flag was unsuitable because it would incorrectly apply strictness to shared, reusable schemas.
|
||||
|
||||
- **Solution:** A runtime validation option was implemented to enforce strictness recursively. This required several coordinated changes to the `boon` validator.
|
||||
|
||||
#### Key Changes
|
||||
|
||||
1. **`ValidationOptions` Struct**: A new `ValidationOptions { be_strict: bool }` struct was added to `validator/src/lib.rs`. The `jspg` code in `src/lib.rs` determines if a validation run should be strict and passes this struct to the validator.
|
||||
|
||||
2. **Strictness Check in `uneval_validate`**: The original `boon` only checked for unevaluated properties if the `unevaluatedProperties` keyword was present in the schema. We added an `else if be_strict` block to `uneval_validate` in `validator/src/validator.rs`. This block triggers a check for any leftover unevaluated properties at the end of a validation pass and reports them as errors, effectively enforcing our runtime strictness rule.
|
||||
|
||||
3. **Correct Context Propagation**: The most complex part of the fix was ensuring the set of unevaluated properties was correctly maintained across different validation contexts (especially `$ref` and nested property validations). Three critical changes were made:
|
||||
- **Inheriting Context in `_validate_self`**: When validating keywords that apply to the same instance (like `$ref` or `allOf`), the sub-validator must know what properties the parent has already evaluated. We changed the creation of the `Validator` inside `_validate_self` to pass a clone of the parent's `uneval` state (`uneval: self.uneval.clone()`) instead of creating a new one from scratch. This allows the context to flow downwards.
|
||||
- **Isolating Context in `validate_val`**: Conversely, when validating a property's value, that value is a *different* part of the JSON instance. The sub-validation should not affect the parent's list of unevaluated properties. We fixed this by commenting out the `self.uneval.merge(...)` call in the `validate_val` function.
|
||||
- **Simplifying `Uneval::merge`**: The original logic for merging `uneval` state was different for `$ref` keywords. This was incorrect. We simplified the `merge` function to *always* perform an intersection (`retain`), which correctly combines the knowledge of evaluated properties from different schema parts that apply to the same instance.
|
||||
|
||||
4. **Removing Incompatible Assertions**: The changes to context propagation broke several `debug_assert!` macros in the `arr_validate` function, which were part of `boon`'s original design. Since our new validation flow is different but correct, these assertions were removed.
|
||||
The tests are located in `tests/fixtures/*.json` and are executed via `cargo test`.
|
||||
89
build.rs
Normal file
89
build.rs
Normal file
@ -0,0 +1,89 @@
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=tests/fixtures");
|
||||
println!("cargo:rerun-if-changed=Cargo.toml");
|
||||
|
||||
// File 1: src/tests.rs for #[pg_test]
|
||||
let pg_dest_path = Path::new("src/tests.rs");
|
||||
let mut pg_file = File::create(&pg_dest_path).unwrap();
|
||||
|
||||
// File 2: tests/tests.rs for standard #[test] integration
|
||||
let std_dest_path = Path::new("tests/tests.rs");
|
||||
let mut std_file = File::create(&std_dest_path).unwrap();
|
||||
|
||||
// Write headers
|
||||
writeln!(std_file, "use jspg::util;").unwrap();
|
||||
|
||||
// Helper for snake_case conversion
|
||||
let to_snake_case = |s: &str| -> String {
|
||||
s.chars().fold(String::new(), |mut acc, c| {
|
||||
if c.is_uppercase() {
|
||||
if !acc.is_empty() {
|
||||
acc.push('_');
|
||||
}
|
||||
acc.push(c.to_ascii_lowercase());
|
||||
} else if c == '-' || c == ' ' || c == '.' || c == '/' || c == ':' {
|
||||
acc.push('_');
|
||||
} else if c.is_alphanumeric() {
|
||||
acc.push(c);
|
||||
}
|
||||
acc
|
||||
})
|
||||
};
|
||||
|
||||
// Walk tests/fixtures directly
|
||||
let fixtures_path = "tests/fixtures";
|
||||
if Path::new(fixtures_path).exists() {
|
||||
for entry in fs::read_dir(fixtures_path).unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let path = entry.path();
|
||||
if path.extension().unwrap_or_default() == "json" {
|
||||
let file_name = path.file_stem().unwrap().to_str().unwrap();
|
||||
|
||||
// Parse the JSON file to find blocks
|
||||
let file = File::open(&path).unwrap();
|
||||
let val: serde_json::Value = serde_json::from_reader(file).unwrap();
|
||||
|
||||
if let Some(arr) = val.as_array() {
|
||||
for (i, _item) in arr.iter().enumerate() {
|
||||
// Use short, deterministic names to avoid Postgres 63-byte limit
|
||||
// e.g. test_dynamic_ref_case_0
|
||||
let fn_name = format!("test_{}_case_{}", to_snake_case(file_name), i);
|
||||
|
||||
// Write to src/tests.rs (PG Test)
|
||||
write!(
|
||||
pg_file,
|
||||
r#"
|
||||
#[pg_test]
|
||||
fn {}() {{
|
||||
let path = format!("{{}}/tests/fixtures/{}.json", env!("CARGO_MANIFEST_DIR"));
|
||||
crate::util::run_test_file_at_index(&path, {}).unwrap();
|
||||
}}
|
||||
"#,
|
||||
fn_name, file_name, i
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Write to tests/tests.rs (Std Test)
|
||||
write!(
|
||||
std_file,
|
||||
r#"
|
||||
#[test]
|
||||
fn {}() {{
|
||||
let path = format!("{{}}/tests/fixtures/{}.json", env!("CARGO_MANIFEST_DIR"));
|
||||
util::run_test_file_at_index(&path, {}).unwrap();
|
||||
}}
|
||||
"#,
|
||||
fn_name, file_name, i
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
243
old_code/lib.rs
Normal file
243
old_code/lib.rs
Normal file
@ -0,0 +1,243 @@
|
||||
use pgrx::*;
|
||||
|
||||
pg_module_magic!();
|
||||
|
||||
// mod schema;
|
||||
mod registry;
|
||||
mod validator;
|
||||
mod util;
|
||||
|
||||
use crate::registry::REGISTRY;
|
||||
// use crate::schema::Schema;
|
||||
use crate::validator::{Validator, ValidationOptions};
|
||||
use lazy_static::lazy_static;
|
||||
use serde_json::{json, Value};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
enum SchemaType {
|
||||
Enum,
|
||||
Type,
|
||||
Family,
|
||||
PublicPunc,
|
||||
PrivatePunc,
|
||||
}
|
||||
|
||||
struct CachedSchema {
|
||||
t: SchemaType,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref SCHEMA_META: std::sync::RwLock<HashMap<String, CachedSchema>> = std::sync::RwLock::new(HashMap::new());
|
||||
}
|
||||
|
||||
#[pg_extern(strict)]
|
||||
fn cache_json_schemas(enums: JsonB, types: JsonB, puncs: JsonB) -> JsonB {
|
||||
let mut meta = SCHEMA_META.write().unwrap();
|
||||
let enums_value: Value = enums.0;
|
||||
let types_value: Value = types.0;
|
||||
let puncs_value: Value = puncs.0;
|
||||
|
||||
let mut schemas_to_register = Vec::new();
|
||||
|
||||
// Phase 1: Enums
|
||||
if let Some(enums_array) = enums_value.as_array() {
|
||||
for enum_row in enums_array {
|
||||
if let Some(schemas_raw) = enum_row.get("schemas") {
|
||||
if let Some(schemas_array) = schemas_raw.as_array() {
|
||||
for schema_def in schemas_array {
|
||||
if let Some(schema_id) = schema_def.get("$id").and_then(|v| v.as_str()) {
|
||||
schemas_to_register.push((schema_id.to_string(), schema_def.clone(), SchemaType::Enum));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 2: Types & Hierarchy
|
||||
let mut hierarchy_map: HashMap<String, HashSet<String>> = HashMap::new();
|
||||
if let Some(types_array) = types_value.as_array() {
|
||||
for type_row in types_array {
|
||||
if let Some(schemas_raw) = type_row.get("schemas") {
|
||||
if let Some(schemas_array) = schemas_raw.as_array() {
|
||||
for schema_def in schemas_array {
|
||||
if let Some(schema_id) = schema_def.get("$id").and_then(|v| v.as_str()) {
|
||||
schemas_to_register.push((schema_id.to_string(), schema_def.clone(), SchemaType::Type));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(type_name) = type_row.get("name").and_then(|v| v.as_str()) {
|
||||
if let Some(hierarchy_raw) = type_row.get("hierarchy") {
|
||||
if let Some(hierarchy_array) = hierarchy_raw.as_array() {
|
||||
for ancestor_val in hierarchy_array {
|
||||
if let Some(ancestor_name) = ancestor_val.as_str() {
|
||||
hierarchy_map.entry(ancestor_name.to_string()).or_default().insert(type_name.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (base_type, descendant_types) in hierarchy_map {
|
||||
let family_id = format!("{}.family", base_type);
|
||||
let values: Vec<String> = descendant_types.into_iter().collect();
|
||||
let family_schema = json!({ "$id": family_id, "type": "string", "enum": values });
|
||||
schemas_to_register.push((family_id, family_schema, SchemaType::Family));
|
||||
}
|
||||
|
||||
// Phase 3: Puncs
|
||||
if let Some(puncs_array) = puncs_value.as_array() {
|
||||
for punc_row in puncs_array {
|
||||
if let Some(punc_obj) = punc_row.as_object() {
|
||||
if let Some(punc_name) = punc_obj.get("name").and_then(|v| v.as_str()) {
|
||||
let is_public = punc_obj.get("public").and_then(|v| v.as_bool()).unwrap_or(false);
|
||||
let punc_type = if is_public { SchemaType::PublicPunc } else { SchemaType::PrivatePunc };
|
||||
if let Some(schemas_raw) = punc_obj.get("schemas") {
|
||||
if let Some(schemas_array) = schemas_raw.as_array() {
|
||||
for schema_def in schemas_array {
|
||||
if let Some(schema_id) = schema_def.get("$id").and_then(|v| v.as_str()) {
|
||||
let req_id = format!("{}.request", punc_name);
|
||||
let resp_id = format!("{}.response", punc_name);
|
||||
let st = if schema_id == req_id || schema_id == resp_id { punc_type } else { SchemaType::Type };
|
||||
schemas_to_register.push((schema_id.to_string(), schema_def.clone(), st));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut all_errors = Vec::new();
|
||||
for (id, value, st) in schemas_to_register {
|
||||
// Meta-validation: Check 'type' enum if present
|
||||
if let Some(type_val) = value.get("type") {
|
||||
let types = match type_val {
|
||||
Value::String(s) => vec![s.as_str()],
|
||||
Value::Array(a) => a.iter().filter_map(|v| v.as_str()).collect(),
|
||||
_ => vec![],
|
||||
};
|
||||
let valid_primitives = ["string", "number", "integer", "boolean", "array", "object", "null"];
|
||||
for t in types {
|
||||
if !valid_primitives.contains(&t) {
|
||||
all_errors.push(json!({ "code": "ENUM_VIOLATED", "message": format!("Invalid type: {}", t) }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clone value for insertion since it might be consumed/moved if we were doing other things
|
||||
let value_for_registry = value.clone();
|
||||
|
||||
// Validation: just ensure it is an object or boolean
|
||||
if value.is_object() || value.is_boolean() {
|
||||
REGISTRY.insert(id.clone(), value_for_registry);
|
||||
meta.insert(id, CachedSchema { t: st });
|
||||
} else {
|
||||
all_errors.push(json!({ "code": "INVALID_SCHEMA_TYPE", "message": format!("Schema {} must be an object or boolean", id) }));
|
||||
}
|
||||
}
|
||||
|
||||
if !all_errors.is_empty() {
|
||||
return JsonB(json!({ "errors": all_errors }));
|
||||
}
|
||||
|
||||
JsonB(json!({ "response": "success" }))
|
||||
}
|
||||
|
||||
#[pg_extern(strict, parallel_safe)]
|
||||
fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||
let schema = match REGISTRY.get(schema_id) {
|
||||
Some(s) => s,
|
||||
None => return JsonB(json!({
|
||||
"errors": [{
|
||||
"code": "SCHEMA_NOT_FOUND",
|
||||
"message": format!("Schema '{}' not found", schema_id),
|
||||
"details": { "schema": schema_id }
|
||||
}]
|
||||
})),
|
||||
};
|
||||
|
||||
let meta = SCHEMA_META.read().unwrap();
|
||||
let st = meta.get(schema_id).map(|m| m.t).unwrap_or(SchemaType::Type);
|
||||
|
||||
let be_strict = match st {
|
||||
SchemaType::PublicPunc => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
let options = ValidationOptions {
|
||||
be_strict,
|
||||
};
|
||||
|
||||
let mut validator = Validator::new(options, schema_id);
|
||||
match validator.validate(&schema, &instance.0) {
|
||||
Ok(_) => JsonB(json!({ "response": "success" })),
|
||||
Err(errors) => {
|
||||
let drop_errors: Vec<Value> = errors.into_iter().map(|e| json!({
|
||||
"code": e.code,
|
||||
"message": e.message,
|
||||
"details": {
|
||||
"path": e.path,
|
||||
"context": e.context,
|
||||
"cause": e.cause,
|
||||
"schema": e.schema_id
|
||||
}
|
||||
})).collect();
|
||||
|
||||
if let Ok(mut f) = std::fs::OpenOptions::new().create(true).append(true).open("/tmp/debug_jspg_errors.log") {
|
||||
use std::io::Write;
|
||||
let _ = writeln!(f, "VALIDATION FAILED for {}: {:?}", schema_id, drop_errors);
|
||||
}
|
||||
|
||||
JsonB(json!({ "errors": drop_errors }))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[pg_extern(strict, parallel_safe)]
|
||||
fn json_schema_cached(schema_id: &str) -> bool {
|
||||
REGISTRY.get(schema_id).is_some()
|
||||
}
|
||||
|
||||
#[pg_extern(strict)]
|
||||
fn clear_json_schemas() -> JsonB {
|
||||
REGISTRY.reset();
|
||||
let mut meta = SCHEMA_META.write().unwrap();
|
||||
meta.clear();
|
||||
JsonB(json!({ "response": "success" }))
|
||||
}
|
||||
|
||||
#[pg_extern(strict, parallel_safe)]
|
||||
fn show_json_schemas() -> JsonB {
|
||||
let meta = SCHEMA_META.read().unwrap();
|
||||
let ids: Vec<String> = meta.keys().cloned().collect();
|
||||
JsonB(json!({ "response": ids }))
|
||||
}
|
||||
|
||||
/// This module is required by `cargo pgrx test` invocations.
|
||||
/// It must be visible at the root of your extension crate.
|
||||
#[cfg(test)]
|
||||
pub mod pg_test {
|
||||
pub fn setup(_options: Vec<&str>) {
|
||||
// perform one-off initialization when the pg_test framework starts
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn postgresql_conf_options() -> Vec<&'static str> {
|
||||
// return any postgresql.conf settings that are required for your tests
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
#[pg_schema]
|
||||
mod tests {
|
||||
use pgrx::pg_test;
|
||||
include!("suite.rs");
|
||||
}
|
||||
217
old_code/registry.rs
Normal file
217
old_code/registry.rs
Normal file
@ -0,0 +1,217 @@
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::RwLock;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref REGISTRY: Registry = Registry::new();
|
||||
}
|
||||
|
||||
pub struct Registry {
|
||||
schemas: RwLock<HashMap<String, Value>>,
|
||||
}
|
||||
|
||||
impl Registry {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
schemas: RwLock::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reset(&self) {
|
||||
let mut schemas = self.schemas.write().unwrap();
|
||||
schemas.clear();
|
||||
}
|
||||
|
||||
pub fn insert(&self, id: String, schema: Value) {
|
||||
let mut schemas = self.schemas.write().unwrap();
|
||||
|
||||
// Index the schema and its sub-resources (IDs and anchors)
|
||||
self.index_schema(&schema, &mut schemas, Some(&id));
|
||||
|
||||
// Ensure the root ID is inserted (index_schema handles it, but let's be explicit)
|
||||
schemas.insert(id, schema);
|
||||
}
|
||||
|
||||
fn index_schema(&self, schema: &Value, registry: &mut HashMap<String, Value>, current_scope: Option<&str>) {
|
||||
if let Value::Object(map) = schema {
|
||||
// Only strictly index $id for scope resolution
|
||||
let mut my_scope = current_scope.map(|s| s.to_string());
|
||||
|
||||
if let Some(Value::String(id)) = map.get("$id") {
|
||||
if id.contains("://") {
|
||||
my_scope = Some(id.clone());
|
||||
} else if let Some(scope) = current_scope {
|
||||
if let Some(pos) = scope.rfind('/') {
|
||||
my_scope = Some(format!("{}{}", &scope[..pos + 1], id));
|
||||
} else {
|
||||
my_scope = Some(id.clone());
|
||||
}
|
||||
} else {
|
||||
my_scope = Some(id.clone());
|
||||
}
|
||||
|
||||
if let Some(final_id) = &my_scope {
|
||||
registry.insert(final_id.clone(), schema.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Minimal recursion only for definitions where sub-IDs often live
|
||||
// This is a tradeoff: we don't index EVERYWHERE, but we catch the 90% common case of
|
||||
// bundled definitions without full tree traversal.
|
||||
if let Some(Value::Object(defs)) = map.get("$defs").or_else(|| map.get("definitions")) {
|
||||
for (_, def_schema) in defs {
|
||||
self.index_schema(def_schema, registry, my_scope.as_deref());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, id: &str) -> Option<Value> {
|
||||
let schemas = self.schemas.read().unwrap();
|
||||
schemas.get(id).cloned()
|
||||
}
|
||||
|
||||
pub fn resolve(&self, ref_str: &str, current_id: Option<&str>) -> Option<(Value, String)> {
|
||||
// 1. Try full lookup (Absolute or explicit ID)
|
||||
if let Some(s) = self.get(ref_str) {
|
||||
return Some((s, ref_str.to_string()));
|
||||
}
|
||||
|
||||
// 2. Try Relative lookup against current scope
|
||||
if let Some(curr) = current_id {
|
||||
if let Some(pos) = curr.rfind('/') {
|
||||
let joined = format!("{}{}", &curr[..pos + 1], ref_str);
|
||||
if let Some(s) = self.get(&joined) {
|
||||
return Some((s, joined));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Pointer Resolution
|
||||
// Split into Base URI + Fragment
|
||||
let (base, fragment) = match ref_str.split_once('#') {
|
||||
Some((b, f)) => (b, Some(f)),
|
||||
None => (ref_str, None),
|
||||
};
|
||||
|
||||
// If base is empty, we stay in current schema.
|
||||
// If base is present, we resolve it first.
|
||||
let (root_schema, scope) = if base.is_empty() {
|
||||
if let Some(curr) = current_id {
|
||||
// If we are looking up internally, we rely on the caller having passed the correct current ID
|
||||
// But typically internal refs are just fragments.
|
||||
if let Some(s) = self.get(curr) {
|
||||
(s, curr.to_string())
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
} else {
|
||||
// Resolve external base
|
||||
if let Some(s) = self.get(base) {
|
||||
(s, base.to_string())
|
||||
} else if let Some(curr) = current_id {
|
||||
// Try relative base
|
||||
if let Some(pos) = curr.rfind('/') {
|
||||
let joined = format!("{}{}", &curr[..pos + 1], base);
|
||||
if let Some(s) = self.get(&joined) {
|
||||
(s, joined)
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(frag_raw) = fragment {
|
||||
if frag_raw.is_empty() {
|
||||
return Some((root_schema, scope));
|
||||
}
|
||||
// Decode fragment (it is URI encoded)
|
||||
let frag_cow = percent_encoding::percent_decode_str(frag_raw).decode_utf8().unwrap_or(std::borrow::Cow::Borrowed(frag_raw));
|
||||
let frag = frag_cow.as_ref();
|
||||
|
||||
if frag.starts_with('/') {
|
||||
if let Some(sub) = root_schema.pointer(frag) {
|
||||
return Some((sub.clone(), scope));
|
||||
}
|
||||
} else {
|
||||
// It is an anchor. We scan for it at runtime to avoid complex indexing at insertion.
|
||||
if let Some(sub) = self.find_anchor(&root_schema, frag) {
|
||||
return Some((sub, scope));
|
||||
}
|
||||
}
|
||||
None
|
||||
} else {
|
||||
Some((root_schema, scope))
|
||||
}
|
||||
}
|
||||
|
||||
fn find_anchor(&self, schema: &Value, anchor: &str) -> Option<Value> {
|
||||
match schema {
|
||||
Value::Object(map) => {
|
||||
// Check if this schema itself has the anchor
|
||||
if let Some(Value::String(a)) = map.get("$anchor") {
|
||||
if a == anchor {
|
||||
return Some(schema.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse into $defs / definitions (Map of Schemas)
|
||||
if let Some(Value::Object(defs)) = map.get("$defs").or_else(|| map.get("definitions")) {
|
||||
for val in defs.values() {
|
||||
if let Some(found) = self.find_anchor(val, anchor) { return Some(found); }
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse into properties / patternProperties / dependentSchemas (Map of Schemas)
|
||||
for key in ["properties", "patternProperties", "dependentSchemas"] {
|
||||
if let Some(Value::Object(props)) = map.get(key) {
|
||||
for val in props.values() {
|
||||
if let Some(found) = self.find_anchor(val, anchor) { return Some(found); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse into arrays of schemas
|
||||
for key in ["allOf", "anyOf", "oneOf", "prefixItems"] {
|
||||
if let Some(Value::Array(arr)) = map.get(key) {
|
||||
for item in arr {
|
||||
if let Some(found) = self.find_anchor(item, anchor) { return Some(found); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse into single sub-schemas
|
||||
for key in ["items", "contains", "additionalProperties", "unevaluatedProperties", "not", "if", "then", "else"] {
|
||||
if let Some(val) = map.get(key) {
|
||||
if val.is_object() || val.is_boolean() {
|
||||
if let Some(found) = self.find_anchor(val, anchor) { return Some(found); }
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
Value::Array(arr) => {
|
||||
// Should not happen for a schema object, but if we are passed an array of schemas?
|
||||
// Standard schema is object or bool.
|
||||
// But let's be safe.
|
||||
for item in arr {
|
||||
if let Some(found) = self.find_anchor(item, anchor) {
|
||||
return Some(found);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
236
old_code/suite.rs
Normal file
236
old_code/suite.rs
Normal file
@ -0,0 +1,236 @@
|
||||
// use crate::schema::Schema;
|
||||
use crate::registry::REGISTRY;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{json, Value};
|
||||
use pgrx::JsonB;
|
||||
use std::{fs, path::Path};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct ExpectedError {
|
||||
code: String,
|
||||
path: String,
|
||||
message_contains: Option<String>,
|
||||
cause: Option<Value>,
|
||||
context: Option<Value>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Group {
|
||||
description: String,
|
||||
schema: Option<Value>,
|
||||
enums: Option<Value>,
|
||||
types: Option<Value>,
|
||||
puncs: Option<Value>,
|
||||
tests: Vec<TestCase>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct TestCase {
|
||||
description: String,
|
||||
data: Value,
|
||||
valid: bool,
|
||||
action: Option<String>,
|
||||
schema_id: Option<String>,
|
||||
expect_errors: Option<Vec<ExpectedError>>,
|
||||
}
|
||||
|
||||
include!("tests.rs");
|
||||
|
||||
fn load_remotes(dir: &Path, base_url: &str) {
|
||||
if !dir.exists() { return; }
|
||||
|
||||
for entry in fs::read_dir(dir).expect("Failed to read remotes directory") {
|
||||
let entry = entry.unwrap();
|
||||
let path = entry.path();
|
||||
let file_name = path.file_name().unwrap().to_str().unwrap();
|
||||
|
||||
if path.is_file() && file_name.ends_with(".json") {
|
||||
let content = fs::read_to_string(&path).expect("Failed to read remote file");
|
||||
if let Ok(schema_value) = serde_json::from_str::<Value>(&content) {
|
||||
// Just check if it's a valid JSON value for a schema (object or bool)
|
||||
if schema_value.is_object() || schema_value.is_boolean() {
|
||||
let schema_id = format!("{}{}", base_url, file_name);
|
||||
REGISTRY.insert(schema_id, schema_value);
|
||||
}
|
||||
}
|
||||
} else if path.is_dir() {
|
||||
load_remotes(&path, &format!("{}{}/", base_url, file_name));
|
||||
}
|
||||
}
|
||||
|
||||
// Mock the meta-schema for testing recursive refs
|
||||
let meta_id = "https://json-schema.org/draft/2020-12/schema";
|
||||
if REGISTRY.get(meta_id).is_none() {
|
||||
// Just mock it as a permissive schema for now so refs resolve
|
||||
REGISTRY.insert(meta_id.to_string(), json!({ "$id": meta_id }));
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn run_dir(dir: &Path, base_url: Option<&str>) -> (usize, usize) {
|
||||
let mut file_count = 0;
|
||||
let mut test_count = 0;
|
||||
|
||||
for entry in fs::read_dir(dir).expect("Failed to read directory") {
|
||||
let entry = entry.unwrap();
|
||||
let path = entry.path();
|
||||
let file_name = path.file_name().unwrap().to_str().unwrap();
|
||||
|
||||
if path.is_file() && file_name.ends_with(".json") {
|
||||
let count = run_file(&path, base_url);
|
||||
test_count += count;
|
||||
file_count += 1;
|
||||
} else if path.is_dir() {
|
||||
if !file_name.starts_with('.') && file_name != "optional" {
|
||||
let (f, t) = run_dir(&path, base_url);
|
||||
file_count += f;
|
||||
test_count += t;
|
||||
}
|
||||
}
|
||||
}
|
||||
(file_count, test_count)
|
||||
}
|
||||
|
||||
fn run_file(path: &Path, base_url: Option<&str>) -> usize {
|
||||
let content = fs::read_to_string(path).expect("Failed to read file");
|
||||
let groups: Vec<Group> = serde_json::from_str(&content).expect("Failed to parse JSON");
|
||||
let filename = path.file_name().unwrap().to_str().unwrap();
|
||||
|
||||
let mut test_count = 0;
|
||||
|
||||
for group in groups {
|
||||
// Handle JSPG setup if any JSPG fields are present
|
||||
if group.enums.is_some() || group.types.is_some() || group.puncs.is_some() {
|
||||
let enums = group.enums.clone().unwrap_or(json!([]));
|
||||
let types = group.types.clone().unwrap_or(json!([]));
|
||||
let puncs = group.puncs.clone().unwrap_or(json!([]));
|
||||
// Use internal helper to register without clearing
|
||||
let result = crate::cache_json_schemas(JsonB(enums), JsonB(types), JsonB(puncs));
|
||||
if let Some(errors) = result.0.get("errors") {
|
||||
// If the group has a test specifically for caching failures, don't panic here
|
||||
let has_cache_test = group.tests.iter().any(|t| t.action.as_deref() == Some("cache"));
|
||||
if !has_cache_test {
|
||||
panic!("FAILED: File: {}, Group: {}\nCache failed: {:?}", filename, group.description, errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut temp_id = "test_root".to_string();
|
||||
if let Some(schema_value) = &group.schema {
|
||||
temp_id = base_url.map(|b| format!("{}schema.json", b)).unwrap_or_else(|| "test_root".to_string());
|
||||
|
||||
if schema_value.is_object() || schema_value.is_boolean() {
|
||||
REGISTRY.insert(temp_id.clone(), schema_value.clone());
|
||||
}
|
||||
} else {
|
||||
// Fallback for JSPG style tests where the schema is in the puncs/types
|
||||
let get_first_id = |items: &Option<Value>| {
|
||||
items.as_ref()
|
||||
.and_then(|v| v.as_array())
|
||||
.and_then(|arr| arr.first())
|
||||
.and_then(|item| item.get("schemas"))
|
||||
.and_then(|v| v.as_array())
|
||||
.and_then(|arr| arr.first())
|
||||
.and_then(|sch| sch.get("$id"))
|
||||
.and_then(|id| id.as_str())
|
||||
.map(|s| s.to_string())
|
||||
};
|
||||
|
||||
if let Some(id) = get_first_id(&group.puncs).or_else(|| get_first_id(&group.types)) {
|
||||
temp_id = id;
|
||||
}
|
||||
}
|
||||
|
||||
for test in &group.tests {
|
||||
test_count += 1;
|
||||
let sid = test.schema_id.clone().unwrap_or_else(|| temp_id.clone());
|
||||
let action = test.action.as_deref().unwrap_or("validate");
|
||||
pgrx::notice!("Starting Test: {}", test.description);
|
||||
|
||||
let result = if action == "cache" {
|
||||
let enums = group.enums.clone().unwrap_or(json!([]));
|
||||
let types = group.types.clone().unwrap_or(json!([]));
|
||||
let puncs = group.puncs.clone().unwrap_or(json!([]));
|
||||
crate::cache_json_schemas(JsonB(enums), JsonB(types), JsonB(puncs))
|
||||
} else {
|
||||
crate::validate_json_schema(&sid, JsonB(test.data.clone()))
|
||||
};
|
||||
let is_success = result.0.get("response").is_some();
|
||||
pgrx::notice!("TEST: file={}, group={}, test={}, valid={}, outcome={}",
|
||||
filename,
|
||||
&group.description,
|
||||
&test.description,
|
||||
test.valid,
|
||||
if is_success { "SUCCESS" } else { "ERRORS" }
|
||||
);
|
||||
|
||||
if is_success != test.valid {
|
||||
if let Some(errs) = result.0.get("errors") {
|
||||
panic!(
|
||||
"FAILED: File: {}, Group: {}, Test: {}\nExpected valid: {}, got ERRORS: {:?}",
|
||||
filename,
|
||||
group.description,
|
||||
test.description,
|
||||
test.valid,
|
||||
errs
|
||||
);
|
||||
} else {
|
||||
panic!(
|
||||
"FAILED: File: {}, Group: {}, Test: {}\nExpected invalid, got SUCCESS",
|
||||
filename,
|
||||
group.description,
|
||||
test.description
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Perform detailed assertions if present
|
||||
if let Some(expectations) = &test.expect_errors {
|
||||
let actual_errors = result.0.get("errors").and_then(|e| e.as_array()).expect("Expected errors array in failure response");
|
||||
|
||||
for expected in expectations {
|
||||
let found = actual_errors.iter().any(|e| {
|
||||
let code = e["code"].as_str().unwrap_or("");
|
||||
let path = e["details"]["path"].as_str().unwrap_or("");
|
||||
let message = e["message"].as_str().unwrap_or("");
|
||||
|
||||
let code_match = code == expected.code;
|
||||
let path_match = path == expected.path;
|
||||
let msg_match = if let Some(sub) = &expected.message_contains {
|
||||
message.contains(sub)
|
||||
} else {
|
||||
true
|
||||
};
|
||||
|
||||
let matches_cause = if let Some(expected_cause) = &expected.cause {
|
||||
e["details"]["cause"] == *expected_cause
|
||||
} else {
|
||||
true
|
||||
};
|
||||
|
||||
let matches_context = if let Some(expected_context) = &expected.context {
|
||||
e["details"]["context"] == *expected_context
|
||||
} else {
|
||||
true
|
||||
};
|
||||
|
||||
code_match && path_match && msg_match && matches_cause && matches_context
|
||||
});
|
||||
|
||||
if !found {
|
||||
panic!(
|
||||
"FAILED: File: {}, Group: {}, Test: {}\nMissing expected error: code='{}', path='{}'\nActual errors: {:?}",
|
||||
filename,
|
||||
group.description,
|
||||
test.description,
|
||||
expected.code,
|
||||
expected.path,
|
||||
actual_errors
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // end of test loop
|
||||
} // end of group loop
|
||||
test_count
|
||||
}
|
||||
482
old_code/tests.rs
Normal file
482
old_code/tests.rs
Normal file
@ -0,0 +1,482 @@
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_additional_properties() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/additionalProperties.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_cache() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/cache.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_const() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/const.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_dependencies() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/dependencies.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_enum() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/enum.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_errors() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/errors.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_format() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/format.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_infinite_loop_detection() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/infinite-loop-detection.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_one_of() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/oneOf.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_properties() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/properties.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_punc() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/punc.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_ref() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/ref.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_required() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/required.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_simple() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/simple.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_strict() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/strict.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_title() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/title.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_type() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/type.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_unevaluated_properties() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/unevaluatedProperties.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_jspg_unique_items() {
|
||||
REGISTRY.reset();
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSPG-Test-Suite/uniqueItems.json"), None);
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_additional_properties() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/additionalProperties.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_all_of() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/allOf.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_anchor() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/anchor.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_any_of() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/anyOf.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_boolean_schema() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/boolean_schema.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_const() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/const.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_contains() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/contains.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_content() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/content.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_default() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/default.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_defs() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/defs.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_dependent_required() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/dependentRequired.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_dependent_schemas() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/dependentSchemas.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_dynamic_ref() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/dynamicRef.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_enum() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/enum.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_exclusive_maximum() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/exclusiveMaximum.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_exclusive_minimum() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/exclusiveMinimum.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_format() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/format.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_if_then_else() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/if-then-else.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_infinite_loop_detection() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/infinite-loop-detection.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_items() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/items.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_max_contains() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/maxContains.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_max_items() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/maxItems.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_max_length() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/maxLength.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_max_properties() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/maxProperties.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_maximum() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/maximum.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_min_contains() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/minContains.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_min_items() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/minItems.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_min_length() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/minLength.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_min_properties() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/minProperties.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_minimum() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/minimum.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_multiple_of() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/multipleOf.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_not() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/not.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_one_of() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/oneOf.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_pattern() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/pattern.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_pattern_properties() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/patternProperties.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_prefix_items() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/prefixItems.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_properties() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/properties.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_property_names() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/propertyNames.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_ref() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/ref.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_ref_remote() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/refRemote.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_required() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/required.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_type() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/type.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_unevaluated_items() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/unevaluatedItems.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_unevaluated_properties() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/unevaluatedProperties.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_unique_items() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/uniqueItems.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_json_schema_vocabulary() {
|
||||
REGISTRY.reset();
|
||||
let remotes_dir = Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/remotes");
|
||||
load_remotes(remotes_dir, "http://localhost:1234/");
|
||||
run_file(Path::new("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/tests/fixtures/JSON-Schema-Test-Suite/tests/draft2020-12/vocabulary.json"), Some("http://localhost:1234/"));
|
||||
}
|
||||
53
old_code/util.rs
Normal file
53
old_code/util.rs
Normal file
@ -0,0 +1,53 @@
|
||||
use serde_json::Value;
|
||||
|
||||
/// serde_json treats 0 and 0.0 not equal. so we cannot simply use v1==v2
|
||||
pub fn equals(v1: &Value, v2: &Value) -> bool {
|
||||
match (v1, v2) {
|
||||
(Value::Null, Value::Null) => true,
|
||||
(Value::Bool(b1), Value::Bool(b2)) => b1 == b2,
|
||||
(Value::Number(n1), Value::Number(n2)) => {
|
||||
if let (Some(n1), Some(n2)) = (n1.as_u64(), n2.as_u64()) {
|
||||
return n1 == n2;
|
||||
}
|
||||
if let (Some(n1), Some(n2)) = (n1.as_i64(), n2.as_i64()) {
|
||||
return n1 == n2;
|
||||
}
|
||||
if let (Some(n1), Some(n2)) = (n1.as_f64(), n2.as_f64()) {
|
||||
return (n1 - n2).abs() < f64::EPSILON;
|
||||
}
|
||||
false
|
||||
}
|
||||
(Value::String(s1), Value::String(s2)) => s1 == s2,
|
||||
(Value::Array(arr1), Value::Array(arr2)) => {
|
||||
if arr1.len() != arr2.len() {
|
||||
return false;
|
||||
}
|
||||
arr1.iter().zip(arr2).all(|(e1, e2)| equals(e1, e2))
|
||||
}
|
||||
(Value::Object(obj1), Value::Object(obj2)) => {
|
||||
if obj1.len() != obj2.len() {
|
||||
return false;
|
||||
}
|
||||
for (k1, v1) in obj1 {
|
||||
if let Some(v2) = obj2.get(k1) {
|
||||
if !equals(v1, v2) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_integer(v: &Value) -> bool {
|
||||
match v {
|
||||
Value::Number(n) => {
|
||||
n.is_i64() || n.is_u64() || n.as_f64().filter(|n| n.fract() == 0.0).is_some()
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
621
old_code/validator.rs
Normal file
621
old_code/validator.rs
Normal file
@ -0,0 +1,621 @@
|
||||
use crate::registry::REGISTRY;
|
||||
use crate::util::{equals, is_integer};
|
||||
use serde_json::{Value, json, Map};
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize)]
|
||||
pub struct ValidationError {
|
||||
pub code: String,
|
||||
pub message: String,
|
||||
pub path: String,
|
||||
pub context: Value,
|
||||
pub cause: Value,
|
||||
pub schema_id: String,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Copy)]
|
||||
pub struct ValidationOptions {
|
||||
pub be_strict: bool,
|
||||
}
|
||||
|
||||
pub struct Validator<'a> {
|
||||
options: ValidationOptions,
|
||||
// The top-level root schema ID we started with
|
||||
root_schema_id: String,
|
||||
// Accumulated errors
|
||||
errors: Vec<ValidationError>,
|
||||
// Max depth to prevent stack overflow
|
||||
max_depth: usize,
|
||||
_phantom: std::marker::PhantomData<&'a ()>,
|
||||
}
|
||||
|
||||
/// Context passed down through the recursion
|
||||
#[derive(Clone)]
|
||||
struct ValidationContext {
|
||||
// Current JSON pointer path in the instance (e.g. "/users/0/name")
|
||||
current_path: String,
|
||||
// The properties overridden by parent schemas (for JSPG inheritance)
|
||||
overrides: HashSet<String>,
|
||||
// Current resolution scope for $ref (changes when following refs)
|
||||
resolution_scope: String,
|
||||
// Current recursion depth
|
||||
depth: usize,
|
||||
}
|
||||
|
||||
impl ValidationContext {
|
||||
fn append_path(&self, extra: &str) -> ValidationContext {
|
||||
let mut new_ctx = self.clone();
|
||||
if new_ctx.current_path.ends_with('/') {
|
||||
new_ctx.current_path.push_str(extra);
|
||||
} else if new_ctx.current_path.is_empty() {
|
||||
new_ctx.current_path.push('/');
|
||||
new_ctx.current_path.push_str(extra);
|
||||
} else {
|
||||
new_ctx.current_path.push('/');
|
||||
new_ctx.current_path.push_str(extra);
|
||||
}
|
||||
new_ctx
|
||||
}
|
||||
|
||||
fn append_path_new_scope(&self, extra: &str) -> ValidationContext {
|
||||
let mut new_ctx = self.append_path(extra);
|
||||
// Structural recursion clears overrides
|
||||
new_ctx.overrides.clear();
|
||||
new_ctx
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Validator<'a> {
|
||||
pub fn new(options: ValidationOptions, root_schema_id: &str) -> Self {
|
||||
Self {
|
||||
options,
|
||||
root_schema_id: root_schema_id.to_string(),
|
||||
errors: Vec::new(),
|
||||
max_depth: 100,
|
||||
_phantom: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate(&mut self, schema: &Value, instance: &Value) -> Result<(), Vec<ValidationError>> {
|
||||
let ctx = ValidationContext {
|
||||
current_path: String::new(),
|
||||
overrides: HashSet::new(),
|
||||
resolution_scope: self.root_schema_id.clone(),
|
||||
depth: 0,
|
||||
};
|
||||
|
||||
// We treat the top-level validate as "not lax" by default, unless specific schema logic says otherwise.
|
||||
let is_lax = !self.options.be_strict;
|
||||
|
||||
self.validate_node(schema, instance, ctx, is_lax, false, false);
|
||||
|
||||
if self.errors.is_empty() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(self.errors.clone())
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_node(
|
||||
&mut self,
|
||||
schema: &Value,
|
||||
instance: &Value,
|
||||
mut ctx: ValidationContext,
|
||||
is_lax: bool,
|
||||
skip_strict: bool,
|
||||
skip_id: bool,
|
||||
) -> HashSet<String> {
|
||||
let mut evaluated = HashSet::new();
|
||||
|
||||
// Recursion limit
|
||||
if ctx.depth > self.max_depth {
|
||||
self.add_error("MAX_DEPTH_REACHED", "Maximum recursion depth exceeded".to_string(), instance, json!({ "depth": ctx.depth }), &ctx);
|
||||
return evaluated;
|
||||
}
|
||||
|
||||
ctx.depth += 1;
|
||||
|
||||
// Handle Boolean Schemas
|
||||
if let Value::Bool(b) = schema {
|
||||
if !b {
|
||||
self.add_error("FALSE_SCHEMA", "Schema is always false".to_string(), instance, Value::Null, &ctx);
|
||||
}
|
||||
return evaluated;
|
||||
}
|
||||
|
||||
let schema_obj = match schema.as_object() {
|
||||
Some(o) => o,
|
||||
None => return evaluated, // Should be object or bool
|
||||
};
|
||||
|
||||
// 1. Update Resolution Scope ($id)
|
||||
if !skip_id {
|
||||
if let Some(Value::String(id)) = schema_obj.get("$id") {
|
||||
if id.contains("://") {
|
||||
ctx.resolution_scope = id.clone();
|
||||
} else {
|
||||
if let Some(pos) = ctx.resolution_scope.rfind('/') {
|
||||
let base = &ctx.resolution_scope[..pos + 1];
|
||||
ctx.resolution_scope = format!("{}{}", base, id);
|
||||
} else {
|
||||
ctx.resolution_scope = id.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Identify Overrides (JSPG Custom Logic)
|
||||
let mut inheritance_ctx = ctx.clone();
|
||||
if let Some(Value::Object(props)) = schema_obj.get("properties") {
|
||||
for (pname, pval) in props {
|
||||
if let Some(Value::Bool(true)) = pval.get("override") {
|
||||
inheritance_ctx.overrides.insert(pname.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Determine Laxness
|
||||
let mut current_lax = is_lax;
|
||||
if let Some(Value::Bool(true)) = schema_obj.get("unevaluatedProperties") { current_lax = true; }
|
||||
if let Some(Value::Bool(true)) = schema_obj.get("additionalProperties") { current_lax = true; }
|
||||
|
||||
// ======== VALIDATION KEYWORDS ========
|
||||
|
||||
// Type
|
||||
if let Some(type_val) = schema_obj.get("type") {
|
||||
if !self.check_type(type_val, instance) {
|
||||
let got = value_type_name(instance);
|
||||
let want_json = serde_json::to_value(type_val).unwrap_or(json!("unknown"));
|
||||
self.add_error("TYPE_MISMATCH", format!("Expected type {:?} but got {}", type_val, got), instance, json!({ "want": type_val, "got": got }), &ctx);
|
||||
}
|
||||
}
|
||||
|
||||
// Enum
|
||||
if let Some(Value::Array(vals)) = schema_obj.get("enum") {
|
||||
if !vals.iter().any(|v| equals(v, instance)) {
|
||||
self.add_error("ENUM_VIOLATED", "Value not in enum".to_string(), instance, json!({ "want": vals }), &ctx);
|
||||
}
|
||||
}
|
||||
|
||||
// Const
|
||||
if let Some(c) = schema_obj.get("const") {
|
||||
if !equals(c, instance) {
|
||||
self.add_error("CONST_VIOLATED", "Value does not match constant".to_string(), instance, json!({ "want": c }), &ctx);
|
||||
}
|
||||
}
|
||||
|
||||
// Object Validation
|
||||
if let Value::Object(obj) = instance {
|
||||
let obj_eval = self.validate_object(schema_obj, obj, instance, &ctx, current_lax);
|
||||
evaluated.extend(obj_eval);
|
||||
}
|
||||
|
||||
// Array Validation
|
||||
if let Value::Array(arr) = instance {
|
||||
self.validate_array(schema_obj, arr, &ctx, current_lax);
|
||||
}
|
||||
|
||||
// Primitive Validation
|
||||
self.validate_primitives(schema_obj, instance, &ctx);
|
||||
|
||||
// Combinators
|
||||
evaluated.extend(self.validate_combinators(schema_obj, instance, &inheritance_ctx, current_lax));
|
||||
|
||||
// Conditionals
|
||||
evaluated.extend(self.validate_conditionals(schema_obj, instance, &inheritance_ctx, current_lax));
|
||||
|
||||
// $ref
|
||||
if let Some(Value::String(ref_str)) = schema_obj.get("$ref") {
|
||||
if let Some((ref_schema, scope_uri)) = REGISTRY.resolve(ref_str, Some(&inheritance_ctx.resolution_scope)) {
|
||||
let mut new_ctx = inheritance_ctx.clone();
|
||||
new_ctx.resolution_scope = scope_uri;
|
||||
let ref_evaluated = self.validate_node(&ref_schema, instance, new_ctx, is_lax, true, true);
|
||||
evaluated.extend(ref_evaluated);
|
||||
} else {
|
||||
self.add_error("SCHEMA_NOT_FOUND", format!("Ref '{}' not found", ref_str), instance, json!({ "ref": ref_str }), &ctx);
|
||||
}
|
||||
}
|
||||
|
||||
// Unevaluated / Strictness Check
|
||||
self.check_unevaluated(schema_obj, instance, &evaluated, &ctx, current_lax, skip_strict);
|
||||
|
||||
evaluated
|
||||
}
|
||||
|
||||
fn validate_object(
|
||||
&mut self,
|
||||
schema: &Map<String, Value>,
|
||||
obj: &Map<String, Value>,
|
||||
instance: &Value,
|
||||
ctx: &ValidationContext,
|
||||
is_lax: bool,
|
||||
) -> HashSet<String> {
|
||||
let mut evaluated = HashSet::new();
|
||||
|
||||
// required
|
||||
if let Some(Value::Array(req)) = schema.get("required") {
|
||||
for field_val in req {
|
||||
if let Some(field) = field_val.as_str() {
|
||||
if !obj.contains_key(field) {
|
||||
self.add_error("REQUIRED_FIELD_MISSING", format!("Required field '{}' is missing", field), &Value::Null, json!({ "want": [field] }), &ctx.append_path(field));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// properties
|
||||
if let Some(Value::Object(props)) = schema.get("properties") {
|
||||
for (pname, psch) in props {
|
||||
if obj.contains_key(pname) {
|
||||
if ctx.overrides.contains(pname) {
|
||||
evaluated.insert(pname.clone());
|
||||
continue;
|
||||
}
|
||||
evaluated.insert(pname.clone());
|
||||
let sub_ctx = ctx.append_path_new_scope(pname);
|
||||
self.validate_node(psch, &obj[pname], sub_ctx, is_lax, false, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// patternProperties
|
||||
if let Some(Value::Object(pprops)) = schema.get("patternProperties") {
|
||||
for (pattern, psch) in pprops {
|
||||
if let Ok(re) = regex::Regex::new(pattern) {
|
||||
for (pname, pval) in obj {
|
||||
if re.is_match(pname) {
|
||||
if ctx.overrides.contains(pname) {
|
||||
evaluated.insert(pname.clone());
|
||||
continue;
|
||||
}
|
||||
evaluated.insert(pname.clone());
|
||||
let sub_ctx = ctx.append_path_new_scope(pname);
|
||||
self.validate_node(psch, pval, sub_ctx, is_lax, false, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// additionalProperties
|
||||
if let Some(apsch) = schema.get("additionalProperties") {
|
||||
if apsch.is_object() || apsch.is_boolean() {
|
||||
for (key, val) in obj {
|
||||
let in_props = schema.get("properties").and_then(|p| p.as_object()).map_or(false, |p| p.contains_key(key));
|
||||
let in_patterns = schema.get("patternProperties").and_then(|p| p.as_object()).map_or(false, |pp| {
|
||||
pp.keys().any(|k| regex::Regex::new(k).map(|re| re.is_match(key)).unwrap_or(false))
|
||||
});
|
||||
|
||||
if !in_props && !in_patterns {
|
||||
evaluated.insert(key.clone());
|
||||
let sub_ctx = ctx.append_path_new_scope(key);
|
||||
self.validate_node(apsch, val, sub_ctx, is_lax, false, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// dependentRequired
|
||||
if let Some(Value::Object(dep_req)) = schema.get("dependentRequired") {
|
||||
for (prop, required_fields_val) in dep_req {
|
||||
if obj.contains_key(prop) {
|
||||
if let Value::Array(required_fields) = required_fields_val {
|
||||
for req_field_val in required_fields {
|
||||
if let Some(req_field) = req_field_val.as_str() {
|
||||
if !obj.contains_key(req_field) {
|
||||
self.add_error("DEPENDENCY_FAILED", format!("Field '{}' is required when '{}' is present", req_field, prop), &Value::Null, json!({ "prop": prop, "missing": [req_field] }), &ctx.append_path(req_field));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// dependentSchemas
|
||||
if let Some(Value::Object(dep_sch)) = schema.get("dependentSchemas") {
|
||||
for (prop, psch) in dep_sch {
|
||||
if obj.contains_key(prop) {
|
||||
let sub_evaluated = self.validate_node(psch, instance, ctx.clone(), is_lax, false, false);
|
||||
evaluated.extend(sub_evaluated);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// legacy dependencies (Draft 4-7 compat)
|
||||
if let Some(Value::Object(deps)) = schema.get("dependencies") {
|
||||
for (prop, dep_val) in deps {
|
||||
if obj.contains_key(prop) {
|
||||
match dep_val {
|
||||
Value::Array(arr) => {
|
||||
for req_val in arr {
|
||||
if let Some(req_field) = req_val.as_str() {
|
||||
if !obj.contains_key(req_field) {
|
||||
self.add_error(
|
||||
"DEPENDENCY_FAILED",
|
||||
format!("Field '{}' is required when '{}' is present", req_field, prop),
|
||||
&Value::Null,
|
||||
json!({ "prop": prop, "missing": [req_field] }),
|
||||
&ctx.append_path(req_field),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Value::Object(_) => {
|
||||
// Schema dependency
|
||||
let sub_evaluated = self.validate_node(dep_val, instance, ctx.clone(), is_lax, false, false);
|
||||
evaluated.extend(sub_evaluated);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// minProperties / maxProperties
|
||||
if let Some(min) = schema.get("minProperties").and_then(|v| v.as_u64()) {
|
||||
if (obj.len() as u64) < min {
|
||||
self.add_error("MIN_PROPERTIES_VIOLATED", format!("Object must have at least {} properties", min), &json!(obj.len()), json!({ "want": min, "got": obj.len() }), ctx);
|
||||
}
|
||||
}
|
||||
if let Some(max) = schema.get("maxProperties").and_then(|v| v.as_u64()) {
|
||||
if (obj.len() as u64) > max {
|
||||
self.add_error("MAX_PROPERTIES_VIOLATED", format!("Object must have at most {} properties", max), &json!(obj.len()), json!({ "want": max, "got": obj.len() }), ctx);
|
||||
}
|
||||
}
|
||||
|
||||
evaluated
|
||||
}
|
||||
|
||||
fn validate_array(
|
||||
&mut self,
|
||||
schema: &Map<String, Value>,
|
||||
arr: &Vec<Value>,
|
||||
ctx: &ValidationContext,
|
||||
is_lax: bool,
|
||||
) {
|
||||
if let Some(min) = schema.get("minItems").and_then(|v| v.as_u64()) {
|
||||
if (arr.len() as u64) < min {
|
||||
self.add_error("MIN_ITEMS_VIOLATED", format!("Array must have at least {} items", min), &json!(arr.len()), json!({ "want": min, "got": arr.len() }), ctx);
|
||||
}
|
||||
}
|
||||
if let Some(max) = schema.get("maxItems").and_then(|v| v.as_u64()) {
|
||||
if (arr.len() as u64) > max {
|
||||
self.add_error("MAX_ITEMS_VIOLATED", format!("Array must have at most {} items", max), &json!(arr.len()), json!({ "want": max, "got": arr.len() }), ctx);
|
||||
}
|
||||
}
|
||||
|
||||
let mut evaluated_index = 0;
|
||||
if let Some(Value::Array(prefix)) = schema.get("prefixItems") {
|
||||
for (i, psch) in prefix.iter().enumerate() {
|
||||
if let Some(item) = arr.get(i) {
|
||||
let sub_ctx = ctx.append_path_new_scope(&i.to_string());
|
||||
self.validate_node(psch, item, sub_ctx, is_lax, false, false);
|
||||
evaluated_index = i + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(items_val) = schema.get("items") {
|
||||
if let Value::Bool(false) = items_val {
|
||||
if arr.len() > evaluated_index {
|
||||
self.add_error("ADDITIONAL_ITEMS_NOT_ALLOWED", "Extra items not allowed".to_string(), &json!(arr.len()), json!({ "got": arr.len() - evaluated_index }), ctx);
|
||||
}
|
||||
} else {
|
||||
// Schema or true
|
||||
for i in evaluated_index..arr.len() {
|
||||
let sub_ctx = ctx.append_path_new_scope(&i.to_string());
|
||||
self.validate_node(items_val, &arr[i], sub_ctx, is_lax, false, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(contains_sch) = schema.get("contains") {
|
||||
let mut matches = 0;
|
||||
for (i, item) in arr.iter().enumerate() {
|
||||
let mut sub = self.branch();
|
||||
let sub_ctx = ctx.append_path_new_scope(&i.to_string());
|
||||
sub.validate_node(contains_sch, item, sub_ctx, is_lax, false, false);
|
||||
if sub.errors.is_empty() {
|
||||
matches += 1;
|
||||
}
|
||||
}
|
||||
if matches == 0 {
|
||||
self.add_error("CONTAINS_FAILED", "No items match 'contains' schema".to_string(), &json!(arr), json!({}), ctx);
|
||||
}
|
||||
if let Some(min) = schema.get("minContains").and_then(|v| v.as_u64()) {
|
||||
if (matches as u64) < min {
|
||||
self.add_error("MIN_CONTAINS_VIOLATED", format!("Expected at least {} items to match 'contains'", min), &json!(arr), json!({ "want": min, "got": matches }), ctx);
|
||||
}
|
||||
}
|
||||
if let Some(max) = schema.get("maxContains").and_then(|v| v.as_u64()) {
|
||||
if (matches as u64) > max {
|
||||
self.add_error("MAX_CONTAINS_VIOLATED", format!("Expected at most {} items to match 'contains'", max), &json!(arr), json!({ "want": max, "got": matches }), ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// uniqueItems
|
||||
if let Some(Value::Bool(true)) = schema.get("uniqueItems") {
|
||||
for i in 0..arr.len() {
|
||||
for j in (i + 1)..arr.len() {
|
||||
if equals(&arr[i], &arr[j]) {
|
||||
self.add_error("UNIQUE_ITEMS_VIOLATED", format!("Array items at indices {} and {} are equal", i, j), &json!(arr), json!({ "got": [i, j] }), ctx);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_primitives(&mut self, schema: &Map<String, Value>, instance: &Value, ctx: &ValidationContext) {
|
||||
if let Some(s) = instance.as_str() {
|
||||
if let Some(min) = schema.get("minLength").and_then(|v| v.as_u64()) {
|
||||
if (s.chars().count() as u64) < min { self.add_error("MIN_LENGTH_VIOLATED", format!("String too short (min {})", min), instance, json!({ "want": min, "got": s.len() }), ctx); }
|
||||
}
|
||||
if let Some(max) = schema.get("maxLength").and_then(|v| v.as_u64()) {
|
||||
if (s.chars().count() as u64) > max { self.add_error("MAX_LENGTH_VIOLATED", format!("String too long (max {})", max), instance, json!({ "want": max, "got": s.len() }), ctx); }
|
||||
}
|
||||
if let Some(Value::String(pat)) = schema.get("pattern") {
|
||||
if let Ok(re) = regex::Regex::new(pat) {
|
||||
if !re.is_match(s) { self.add_error("PATTERN_VIOLATED", format!("String does not match pattern '{}'", pat), instance, json!({ "want": pat, "got": s }), ctx); }
|
||||
}
|
||||
}
|
||||
if let Some(Value::String(fmt)) = schema.get("format") {
|
||||
if !s.is_empty() {
|
||||
match fmt.as_str() {
|
||||
"uuid" => { if uuid::Uuid::parse_str(s).is_err() { self.add_error("FORMAT_INVALID", format!("Value '{}' is not a valid UUID", s), instance, json!({ "format": "uuid" }), ctx); } }
|
||||
"date-time" => { if chrono::DateTime::parse_from_rfc3339(s).is_err() { self.add_error("FORMAT_INVALID", format!("Value '{}' is not a valid date-time", s), instance, json!({ "format": "date-time" }), ctx); } }
|
||||
"email" => { if !s.contains('@') { self.add_error("FORMAT_INVALID", format!("Value '{}' is not a valid email", s), instance, json!({ "format": "email" }), ctx); } }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(n) = instance.as_f64() {
|
||||
if let Some(min) = schema.get("minimum").and_then(|v| v.as_f64()) {
|
||||
if n < min { self.add_error("MINIMUM_VIOLATED", format!("Value {} < minimum {}", n, min), instance, json!({ "want": min, "got": n }), ctx); }
|
||||
}
|
||||
if let Some(max) = schema.get("maximum").and_then(|v| v.as_f64()) {
|
||||
if n > max { self.add_error("MAXIMUM_VIOLATED", format!("Value {} > maximum {}", n, max), instance, json!({ "want": max, "got": n }), ctx); }
|
||||
}
|
||||
if let Some(min) = schema.get("exclusiveMinimum").and_then(|v| v.as_f64()) {
|
||||
if n <= min { self.add_error("EXCLUSIVE_MINIMUM_VIOLATED", format!("Value {} <= exclusive minimum {}", n, min), instance, json!({ "want": min, "got": n }), ctx); }
|
||||
}
|
||||
if let Some(max) = schema.get("exclusiveMaximum").and_then(|v| v.as_f64()) {
|
||||
if n >= max { self.add_error("EXCLUSIVE_MAXIMUM_VIOLATED", format!("Value {} >= exclusive maximum {}", n, max), instance, json!({ "want": max, "got": n }), ctx); }
|
||||
}
|
||||
if let Some(mult) = schema.get("multipleOf").and_then(|v| v.as_f64()) {
|
||||
let rem = (n / mult).fract();
|
||||
if rem.abs() > f64::EPSILON && (1.0 - rem).abs() > f64::EPSILON {
|
||||
self.add_error("MULTIPLE_OF_VIOLATED", format!("Value {} not multiple of {}", n, mult), instance, json!({ "want": mult, "got": n }), ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_combinators(&mut self, schema: &Map<String, Value>, instance: &Value, ctx: &ValidationContext, is_lax: bool) -> HashSet<String> {
|
||||
let mut evaluated = HashSet::new();
|
||||
if let Some(Value::Array(all_of)) = schema.get("allOf") {
|
||||
for sch in all_of { evaluated.extend(self.validate_node(sch, instance, ctx.clone(), is_lax, true, false)); }
|
||||
}
|
||||
if let Some(Value::Array(any_of)) = schema.get("anyOf") {
|
||||
let mut matched = false;
|
||||
let mut errors_acc = Vec::new();
|
||||
for sch in any_of {
|
||||
let mut sub = self.branch();
|
||||
let sub_eval = sub.validate_node(sch, instance, ctx.clone(), is_lax, false, false);
|
||||
if sub.errors.is_empty() { matched = true; evaluated.extend(sub_eval); } else { errors_acc.extend(sub.errors); }
|
||||
}
|
||||
if !matched { self.add_error("ANY_OF_VIOLATED", "Value did not match any allowed schema".to_string(), instance, json!({ "causes": errors_acc }), ctx); }
|
||||
}
|
||||
if let Some(Value::Array(one_of)) = schema.get("oneOf") {
|
||||
let mut match_count = 0;
|
||||
let mut last_eval = HashSet::new();
|
||||
let mut error_causes = Vec::new();
|
||||
for sch in one_of {
|
||||
let mut sub = self.branch();
|
||||
let sub_eval = sub.validate_node(sch, instance, ctx.clone(), is_lax, false, false);
|
||||
if sub.errors.is_empty() { match_count += 1; last_eval = sub_eval; } else { error_causes.extend(sub.errors); }
|
||||
}
|
||||
if match_count == 1 { evaluated.extend(last_eval); }
|
||||
else { self.add_error("ONE_OF_VIOLATED", format!("Value matched {} schemas, expected 1", match_count), instance, json!({ "matched": match_count, "causes": error_causes }), ctx); }
|
||||
}
|
||||
if let Some(not_sch) = schema.get("not") {
|
||||
let mut sub = self.branch();
|
||||
sub.validate_node(not_sch, instance, ctx.clone(), is_lax, false, false);
|
||||
if sub.errors.is_empty() { self.add_error("NOT_VIOLATED", "Value matched 'not' schema".to_string(), instance, Value::Null, ctx); }
|
||||
}
|
||||
evaluated
|
||||
}
|
||||
|
||||
fn validate_conditionals(&mut self, schema: &Map<String, Value>, instance: &Value, ctx: &ValidationContext, is_lax: bool) -> HashSet<String> {
|
||||
let mut evaluated = HashSet::new();
|
||||
if let Some(if_sch) = schema.get("if") {
|
||||
let mut sub = self.branch();
|
||||
let sub_eval = sub.validate_node(if_sch, instance, ctx.clone(), is_lax, true, false);
|
||||
if sub.errors.is_empty() {
|
||||
evaluated.extend(sub_eval);
|
||||
if let Some(then_sch) = schema.get("then") { evaluated.extend(self.validate_node(then_sch, instance, ctx.clone(), is_lax, false, false)); }
|
||||
} else if let Some(else_sch) = schema.get("else") {
|
||||
evaluated.extend(self.validate_node(else_sch, instance, ctx.clone(), is_lax, false, false));
|
||||
}
|
||||
}
|
||||
evaluated
|
||||
}
|
||||
|
||||
fn check_unevaluated(&mut self, schema: &Map<String, Value>, instance: &Value, evaluated: &HashSet<String>, ctx: &ValidationContext, is_lax: bool, skip_strict: bool) {
|
||||
if let Value::Object(obj) = instance {
|
||||
if let Some(Value::Bool(false)) = schema.get("additionalProperties") {
|
||||
for key in obj.keys() {
|
||||
let in_props = schema.get("properties").and_then(|p| p.as_object()).map_or(false, |p| p.contains_key(key));
|
||||
let in_pattern = schema.get("patternProperties").and_then(|p| p.as_object()).map_or(false, |pp| pp.keys().any(|k| regex::Regex::new(k).map(|re| re.is_match(key)).unwrap_or(false)));
|
||||
if !in_props && !in_pattern {
|
||||
if ctx.overrides.contains(key) { continue; }
|
||||
self.add_error("ADDITIONAL_PROPERTIES_NOT_ALLOWED", format!("Property '{}' is not allowed", key), &Value::Null, json!({ "got": [key] }), &ctx.append_path(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let explicit_opts = schema.contains_key("unevaluatedProperties") || schema.contains_key("additionalProperties");
|
||||
let should_check_strict = self.options.be_strict && !is_lax && !explicit_opts && !skip_strict;
|
||||
let check_unevaluated = matches!(schema.get("unevaluatedProperties"), Some(Value::Bool(false)));
|
||||
if should_check_strict || check_unevaluated {
|
||||
for key in obj.keys() {
|
||||
if !evaluated.contains(key) {
|
||||
if ctx.overrides.contains(key) { continue; }
|
||||
self.add_error("ADDITIONAL_PROPERTIES_NOT_ALLOWED", format!("Property '{}' is not allowed (strict/unevaluated)", key), &Value::Null, json!({ "got": [key] }), &ctx.append_path(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_type(&self, expected: &Value, instance: &Value) -> bool {
|
||||
match expected {
|
||||
Value::String(s) => self.is_primitive_type(s, instance),
|
||||
Value::Array(arr) => arr.iter().filter_map(|v| v.as_str()).any(|pt| self.is_primitive_type(pt, instance)),
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
fn is_primitive_type(&self, pt: &str, instance: &Value) -> bool {
|
||||
match pt {
|
||||
"string" => instance.is_string(),
|
||||
"number" => instance.is_number(),
|
||||
"integer" => is_integer(instance),
|
||||
"boolean" => instance.is_boolean(),
|
||||
"array" => instance.is_array(),
|
||||
"object" => instance.is_object(),
|
||||
"null" => instance.is_null(),
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
fn branch(&self) -> Self {
|
||||
Self { options: self.options, root_schema_id: self.root_schema_id.clone(), errors: Vec::new(), max_depth: self.max_depth, _phantom: std::marker::PhantomData }
|
||||
}
|
||||
|
||||
fn add_error(&mut self, code: &str, message: String, context: &Value, cause: Value, ctx: &ValidationContext) {
|
||||
let path = ctx.current_path.clone();
|
||||
if self.errors.iter().any(|e| e.code == code && e.path == path) { return; }
|
||||
self.errors.push(ValidationError { code: code.to_string(), message, path, context: context.clone(), cause, schema_id: self.root_schema_id.clone() });
|
||||
}
|
||||
|
||||
fn extend_unique(&mut self, errors: Vec<ValidationError>) {
|
||||
for e in errors { if !self.errors.iter().any(|existing| existing.code == e.code && existing.path == e.path) { self.errors.push(e); } }
|
||||
}
|
||||
}
|
||||
|
||||
fn value_type_name(v: &Value) -> &'static str {
|
||||
match v {
|
||||
Value::Null => "null",
|
||||
Value::Bool(_) => "boolean",
|
||||
Value::Number(n) => if n.is_i64() { "integer" } else { "number" },
|
||||
Value::String(_) => "string",
|
||||
Value::Array(_) => "array",
|
||||
Value::Object(_) => "object",
|
||||
}
|
||||
}
|
||||
0
src/helpers.rs → old_tests/helpers.rs
Normal file → Executable file
0
src/helpers.rs → old_tests/helpers.rs
Normal file → Executable file
0
src/schemas.rs → old_tests/schemas.rs
Normal file → Executable file
0
src/schemas.rs → old_tests/schemas.rs
Normal file → Executable file
1089
old_tests/tests.rs
Executable file
1089
old_tests/tests.rs
Executable file
File diff suppressed because it is too large
Load Diff
395
src/compiler.rs
Normal file
395
src/compiler.rs
Normal file
@ -0,0 +1,395 @@
|
||||
use crate::schema::Schema;
|
||||
use regex::Regex;
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Represents a compiled format validator
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum CompiledFormat {
|
||||
/// A simple function pointer validator
|
||||
Func(fn(&Value) -> Result<(), Box<dyn Error + Send + Sync>>),
|
||||
/// A regex-based validator
|
||||
Regex(Regex),
|
||||
}
|
||||
|
||||
/// A fully compiled schema with a root node and a pre-calculated index map.
|
||||
/// This allows O(1) lookup of any anchor or $id within the schema tree.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CompiledSchema {
|
||||
pub root: Arc<Schema>,
|
||||
pub index: HashMap<String, Arc<Schema>>,
|
||||
}
|
||||
|
||||
/// A wrapper for compiled regex patterns
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CompiledRegex(pub Regex);
|
||||
|
||||
/// The Compiler is responsible for pre-calculating high-cost schema operations
|
||||
pub struct Compiler;
|
||||
|
||||
impl Compiler {
|
||||
/// Internal: Compiles formats and regexes in-place
|
||||
fn compile_formats_and_regexes(schema: &mut Schema) {
|
||||
// 1. Compile Format
|
||||
if let Some(format_str) = &schema.format {
|
||||
if let Some(fmt) = crate::formats::FORMATS.get(format_str.as_str()) {
|
||||
schema.compiled_format = Some(CompiledFormat::Func(fmt.func));
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Compile Pattern (regex)
|
||||
if let Some(pattern_str) = &schema.pattern {
|
||||
if let Ok(re) = Regex::new(pattern_str) {
|
||||
schema.compiled_pattern = Some(CompiledRegex(re));
|
||||
}
|
||||
}
|
||||
|
||||
// 2.5 Compile Pattern Properties
|
||||
if let Some(pp) = &schema.pattern_properties {
|
||||
let mut compiled_pp = Vec::new();
|
||||
for (pattern, sub_schema) in pp {
|
||||
if let Ok(re) = Regex::new(pattern) {
|
||||
compiled_pp.push((CompiledRegex(re), sub_schema.clone()));
|
||||
} else {
|
||||
eprintln!(
|
||||
"Invalid patternProperty regex in schema (compile time): {}",
|
||||
pattern
|
||||
);
|
||||
}
|
||||
}
|
||||
if !compiled_pp.is_empty() {
|
||||
schema.compiled_pattern_properties = Some(compiled_pp);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Recurse
|
||||
Self::compile_recursive(schema);
|
||||
}
|
||||
|
||||
fn normalize_dependencies(schema: &mut Schema) {
|
||||
if let Some(deps) = schema.dependencies.take() {
|
||||
for (key, dep) in deps {
|
||||
match dep {
|
||||
crate::schema::Dependency::Props(props) => {
|
||||
schema
|
||||
.dependent_required
|
||||
.get_or_insert_with(std::collections::BTreeMap::new)
|
||||
.insert(key, props);
|
||||
}
|
||||
crate::schema::Dependency::Schema(sub_schema) => {
|
||||
schema
|
||||
.dependent_schemas
|
||||
.get_or_insert_with(std::collections::BTreeMap::new)
|
||||
.insert(key, sub_schema);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn compile_recursive(schema: &mut Schema) {
|
||||
Self::normalize_dependencies(schema);
|
||||
|
||||
// Compile self
|
||||
if let Some(format_str) = &schema.format {
|
||||
if let Some(fmt) = crate::formats::FORMATS.get(format_str.as_str()) {
|
||||
schema.compiled_format = Some(CompiledFormat::Func(fmt.func));
|
||||
}
|
||||
}
|
||||
if let Some(pattern_str) = &schema.pattern {
|
||||
if let Ok(re) = Regex::new(pattern_str) {
|
||||
schema.compiled_pattern = Some(CompiledRegex(re));
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse
|
||||
|
||||
if let Some(defs) = &mut schema.definitions {
|
||||
for s in defs.values_mut() {
|
||||
Self::compile_recursive(Arc::make_mut(s));
|
||||
}
|
||||
}
|
||||
if let Some(defs) = &mut schema.defs {
|
||||
for s in defs.values_mut() {
|
||||
Self::compile_recursive(Arc::make_mut(s));
|
||||
}
|
||||
}
|
||||
if let Some(props) = &mut schema.properties {
|
||||
for s in props.values_mut() {
|
||||
Self::compile_recursive(Arc::make_mut(s));
|
||||
}
|
||||
}
|
||||
|
||||
// ... Recurse logic ...
|
||||
if let Some(items) = &mut schema.items {
|
||||
Self::compile_recursive(Arc::make_mut(items));
|
||||
}
|
||||
if let Some(prefix_items) = &mut schema.prefix_items {
|
||||
for s in prefix_items {
|
||||
Self::compile_recursive(Arc::make_mut(s));
|
||||
}
|
||||
}
|
||||
if let Some(not) = &mut schema.not {
|
||||
Self::compile_recursive(Arc::make_mut(not));
|
||||
}
|
||||
if let Some(all_of) = &mut schema.all_of {
|
||||
for s in all_of {
|
||||
Self::compile_recursive(Arc::make_mut(s));
|
||||
}
|
||||
}
|
||||
if let Some(any_of) = &mut schema.any_of {
|
||||
for s in any_of {
|
||||
Self::compile_recursive(Arc::make_mut(s));
|
||||
}
|
||||
}
|
||||
if let Some(one_of) = &mut schema.one_of {
|
||||
for s in one_of {
|
||||
Self::compile_recursive(Arc::make_mut(s));
|
||||
}
|
||||
}
|
||||
if let Some(s) = &mut schema.if_ {
|
||||
Self::compile_recursive(Arc::make_mut(s));
|
||||
}
|
||||
if let Some(s) = &mut schema.then_ {
|
||||
Self::compile_recursive(Arc::make_mut(s));
|
||||
}
|
||||
if let Some(s) = &mut schema.else_ {
|
||||
Self::compile_recursive(Arc::make_mut(s));
|
||||
}
|
||||
|
||||
if let Some(ds) = &mut schema.dependent_schemas {
|
||||
for s in ds.values_mut() {
|
||||
Self::compile_recursive(Arc::make_mut(s));
|
||||
}
|
||||
}
|
||||
if let Some(pn) = &mut schema.property_names {
|
||||
Self::compile_recursive(Arc::make_mut(pn));
|
||||
}
|
||||
}
|
||||
|
||||
/// Recursively traverses the schema tree to build a map of all internal Anchors ($id) and JSON Pointers.
|
||||
fn compile_index(
|
||||
schema: &Arc<Schema>,
|
||||
index: &mut HashMap<String, Arc<Schema>>,
|
||||
parent_base: Option<String>,
|
||||
pointer: json_pointer::JsonPointer<String, Vec<String>>,
|
||||
) {
|
||||
// 1. Index using Parent Base (Path from Parent)
|
||||
if let Some(base) = &parent_base {
|
||||
// We use the pointer's string representation (e.g., "/definitions/foo")
|
||||
// and append it to the base.
|
||||
let fragment = pointer.to_string();
|
||||
let ptr_uri = if fragment.is_empty() {
|
||||
base.clone()
|
||||
} else {
|
||||
format!("{}#{}", base, fragment)
|
||||
};
|
||||
index.insert(ptr_uri, schema.clone());
|
||||
}
|
||||
|
||||
// 2. Determine Current Scope... (unchanged logic, just use pointer)
|
||||
let mut current_base = parent_base.clone();
|
||||
let mut child_pointer = pointer.clone();
|
||||
|
||||
if let Some(id) = &schema.obj.id {
|
||||
// ... resolve ID logic ...
|
||||
let mut new_base = None;
|
||||
if let Ok(_) = url::Url::parse(id) {
|
||||
new_base = Some(id.clone());
|
||||
} else if let Some(base) = ¤t_base {
|
||||
if let Ok(base_url) = url::Url::parse(base) {
|
||||
if let Ok(joined) = base_url.join(id) {
|
||||
new_base = Some(joined.to_string());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
new_base = Some(id.clone());
|
||||
}
|
||||
|
||||
if let Some(base) = new_base {
|
||||
index.insert(base.clone(), schema.clone());
|
||||
current_base = Some(base);
|
||||
child_pointer = json_pointer::JsonPointer::new(vec![]); // Reset
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Index by Anchor (unchanged)
|
||||
if let Some(anchor) = &schema.obj.anchor {
|
||||
if let Some(base) = ¤t_base {
|
||||
let anchor_uri = format!("{}#{}", base, anchor);
|
||||
index.insert(anchor_uri, schema.clone());
|
||||
}
|
||||
}
|
||||
// Index by Dynamic Anchor
|
||||
if let Some(d_anchor) = &schema.obj.dynamic_anchor {
|
||||
if let Some(base) = ¤t_base {
|
||||
let anchor_uri = format!("{}#{}", base, d_anchor);
|
||||
index.insert(anchor_uri.clone(), schema.clone());
|
||||
println!("Indexed Dynamic Anchor: {}", anchor_uri);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Index by Anchor
|
||||
if let Some(anchor) = &schema.obj.anchor {
|
||||
if let Some(base) = ¤t_base {
|
||||
let anchor_uri = format!("{}#{}", base, anchor);
|
||||
index.insert(anchor_uri.clone(), schema.clone());
|
||||
println!("Indexed Anchor: {}", anchor_uri);
|
||||
}
|
||||
}
|
||||
|
||||
// ... (Const/Enum indexing skipped for brevity, relies on string)
|
||||
|
||||
// 4. Recurse
|
||||
if let Some(defs) = schema.defs.as_ref().or(schema.definitions.as_ref()) {
|
||||
let segment = if schema.defs.is_some() {
|
||||
"$defs"
|
||||
} else {
|
||||
"definitions"
|
||||
};
|
||||
for (key, sub_schema) in defs {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push(segment.to_string());
|
||||
// Decode key to avoid double encoding by JsonPointer
|
||||
let decoded_key = percent_encoding::percent_decode_str(key).decode_utf8_lossy();
|
||||
sub.push(decoded_key.to_string());
|
||||
Self::compile_index(sub_schema, index, current_base.clone(), sub);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(props) = &schema.properties {
|
||||
for (key, sub_schema) in props {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push("properties".to_string());
|
||||
sub.push(key.to_string());
|
||||
Self::compile_index(sub_schema, index, current_base.clone(), sub);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(items) = &schema.items {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push("items".to_string());
|
||||
Self::compile_index(items, index, current_base.clone(), sub);
|
||||
}
|
||||
|
||||
if let Some(prefix_items) = &schema.prefix_items {
|
||||
for (i, sub_schema) in prefix_items.iter().enumerate() {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push("prefixItems".to_string());
|
||||
sub.push(i.to_string());
|
||||
Self::compile_index(sub_schema, index, current_base.clone(), sub);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(all_of) = &schema.all_of {
|
||||
for (i, sub_schema) in all_of.iter().enumerate() {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push("allOf".to_string());
|
||||
sub.push(i.to_string());
|
||||
Self::compile_index(sub_schema, index, current_base.clone(), sub);
|
||||
}
|
||||
}
|
||||
if let Some(any_of) = &schema.any_of {
|
||||
for (i, sub_schema) in any_of.iter().enumerate() {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push("anyOf".to_string());
|
||||
sub.push(i.to_string());
|
||||
Self::compile_index(sub_schema, index, current_base.clone(), sub);
|
||||
}
|
||||
}
|
||||
if let Some(one_of) = &schema.one_of {
|
||||
for (i, sub_schema) in one_of.iter().enumerate() {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push("oneOf".to_string());
|
||||
sub.push(i.to_string());
|
||||
Self::compile_index(sub_schema, index, current_base.clone(), sub);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(not) = &schema.not {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push("not".to_string());
|
||||
Self::compile_index(not, index, current_base.clone(), sub);
|
||||
}
|
||||
if let Some(if_) = &schema.if_ {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push("if".to_string());
|
||||
Self::compile_index(if_, index, current_base.clone(), sub);
|
||||
}
|
||||
if let Some(then_) = &schema.then_ {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push("then".to_string());
|
||||
Self::compile_index(then_, index, current_base.clone(), sub);
|
||||
}
|
||||
if let Some(else_) = &schema.else_ {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push("else".to_string());
|
||||
Self::compile_index(else_, index, current_base.clone(), sub);
|
||||
}
|
||||
if let Some(deps) = &schema.dependent_schemas {
|
||||
for (key, sub_schema) in deps {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push("dependentSchemas".to_string());
|
||||
sub.push(key.to_string());
|
||||
Self::compile_index(sub_schema, index, current_base.clone(), sub);
|
||||
}
|
||||
}
|
||||
if let Some(pp) = &schema.pattern_properties {
|
||||
for (key, sub_schema) in pp {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push("patternProperties".to_string());
|
||||
sub.push(key.to_string());
|
||||
Self::compile_index(sub_schema, index, current_base.clone(), sub);
|
||||
}
|
||||
}
|
||||
if let Some(contains) = &schema.contains {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push("contains".to_string());
|
||||
Self::compile_index(contains, index, current_base.clone(), sub);
|
||||
}
|
||||
if let Some(property_names) = &schema.property_names {
|
||||
let mut sub = child_pointer.clone();
|
||||
sub.push("propertyNames".to_string());
|
||||
Self::compile_index(property_names, index, current_base.clone(), sub);
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolves a format string to a CompiledFormat (future optimization)
|
||||
pub fn compile_format(_format: &str) -> Option<CompiledFormat> {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn compile(mut root_schema: Schema, root_id: Option<String>) -> CompiledSchema {
|
||||
// 1. Compile in-place (formats/regexes)
|
||||
Self::compile_formats_and_regexes(&mut root_schema);
|
||||
|
||||
// Apply root_id override if schema ID is missing
|
||||
if let Some(ref rid) = root_id {
|
||||
if root_schema.obj.id.is_none() {
|
||||
root_schema.obj.id = Some(rid.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Wrap in Arc
|
||||
let root = Arc::new(root_schema);
|
||||
let mut index = HashMap::new();
|
||||
|
||||
// 3. Build ID/Pointer Index
|
||||
// Default base_uri to "" so that pointers like "#/foo" are indexed even if no root ID exists
|
||||
Self::compile_index(
|
||||
&root,
|
||||
&mut index,
|
||||
root_id.clone().or(Some("".to_string())),
|
||||
json_pointer::JsonPointer::new(vec![]),
|
||||
);
|
||||
|
||||
// Also ensure root id is indexed if present
|
||||
if let Some(rid) = root_id {
|
||||
index.insert(rid, root.clone());
|
||||
}
|
||||
|
||||
CompiledSchema { root, index }
|
||||
}
|
||||
}
|
||||
61
src/drop.rs
Normal file
61
src/drop.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Drop {
|
||||
// We don't need id, frequency, etc. for the validation result specifically,
|
||||
// as they are added by the SQL wrapper. We just need to conform to the structure.
|
||||
// The user said "Validator::validate always needs to return this drop type".
|
||||
// So we should match it as closely as possible.
|
||||
|
||||
#[serde(rename = "type")]
|
||||
pub type_: String, // "drop"
|
||||
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub response: Option<Value>,
|
||||
|
||||
#[serde(default)]
|
||||
pub errors: Vec<Error>,
|
||||
}
|
||||
|
||||
impl Drop {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
type_: "drop".to_string(),
|
||||
response: None,
|
||||
errors: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn success() -> Self {
|
||||
Self {
|
||||
type_: "drop".to_string(),
|
||||
response: Some(serde_json::json!({ "result": "success" })), // Or appropriate success response
|
||||
errors: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_errors(errors: Vec<Error>) -> Self {
|
||||
Self {
|
||||
type_: "drop".to_string(),
|
||||
response: None,
|
||||
errors,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Error {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub punc: Option<String>,
|
||||
pub code: String,
|
||||
pub message: String,
|
||||
pub details: ErrorDetails,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct ErrorDetails {
|
||||
pub path: String,
|
||||
// Extensions can be added here (package, cause, etc)
|
||||
// For now, validator only provides path
|
||||
}
|
||||
875
src/formats.rs
Normal file
875
src/formats.rs
Normal file
@ -0,0 +1,875 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
error::Error,
|
||||
net::{Ipv4Addr, Ipv6Addr},
|
||||
};
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use percent_encoding::percent_decode_str;
|
||||
use serde_json::Value;
|
||||
use url::Url;
|
||||
|
||||
// use crate::ecma; // Assuming ecma is not yet available, stubbing regex for now
|
||||
|
||||
/// Defines format for `format` keyword.
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Format {
|
||||
/// Name of the format
|
||||
pub name: &'static str,
|
||||
|
||||
/// validates given value.
|
||||
pub func: fn(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>>, // Ensure thread safety if needed
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub(crate) static ref FORMATS: HashMap<&'static str, Format> = {
|
||||
let mut m = HashMap::<&'static str, Format>::new();
|
||||
// Helper to register formats
|
||||
let mut register = |name, func| m.insert(name, Format { name, func });
|
||||
|
||||
// register("regex", validate_regex); // Stubbed
|
||||
register("ipv4", validate_ipv4);
|
||||
register("ipv6", validate_ipv6);
|
||||
register("hostname", validate_hostname);
|
||||
register("idn-hostname", validate_idn_hostname);
|
||||
register("email", validate_email);
|
||||
register("idn-email", validate_idn_email);
|
||||
register("date", validate_date);
|
||||
register("time", validate_time);
|
||||
register("date-time", validate_date_time);
|
||||
register("duration", validate_duration);
|
||||
register("period", validate_period);
|
||||
register("json-pointer", validate_json_pointer);
|
||||
register("relative-json-pointer", validate_relative_json_pointer);
|
||||
register("uuid", validate_uuid);
|
||||
register("uri", validate_uri);
|
||||
register("iri", validate_iri);
|
||||
register("uri-reference", validate_uri_reference);
|
||||
register("iri-reference", validate_iri_reference);
|
||||
register("uri-template", validate_uri_template);
|
||||
m
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
fn validate_regex(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
// ecma::convert(s).map(|_| ())
|
||||
Ok(())
|
||||
}
|
||||
*/
|
||||
|
||||
fn validate_ipv4(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
s.parse::<Ipv4Addr>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_ipv6(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
s.parse::<Ipv6Addr>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_date(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
check_date(s)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn matches_char(s: &str, index: usize, ch: char) -> bool {
|
||||
s.is_char_boundary(index) && s[index..].starts_with(ch)
|
||||
}
|
||||
|
||||
// see https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
|
||||
fn check_date(s: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
// yyyy-mm-dd
|
||||
if s.len() != 10 {
|
||||
Err("must be 10 characters long")?;
|
||||
}
|
||||
if !matches_char(s, 4, '-') || !matches_char(s, 7, '-') {
|
||||
Err("missing hyphen in correct place")?;
|
||||
}
|
||||
|
||||
let mut ymd = s.splitn(3, '-').filter_map(|t| t.parse::<usize>().ok());
|
||||
let (Some(y), Some(m), Some(d)) = (ymd.next(), ymd.next(), ymd.next()) else {
|
||||
Err("non-positive year/month/day")?
|
||||
};
|
||||
|
||||
if !matches!(m, 1..=12) {
|
||||
Err(format!("{m} months in year"))?;
|
||||
}
|
||||
if !matches!(d, 1..=31) {
|
||||
Err(format!("{d} days in month"))?;
|
||||
}
|
||||
|
||||
match m {
|
||||
2 => {
|
||||
let mut feb_days = 28;
|
||||
if y % 4 == 0 && (y % 100 != 0 || y % 400 == 0) {
|
||||
feb_days += 1; // leap year
|
||||
};
|
||||
if d > feb_days {
|
||||
Err(format!("february has {feb_days} days only"))?;
|
||||
}
|
||||
}
|
||||
4 | 6 | 9 | 11 => {
|
||||
if d > 30 {
|
||||
Err("month has 30 days only")?;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_time(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
check_time(s)
|
||||
}
|
||||
|
||||
fn check_time(mut str: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
// min: hh:mm:ssZ
|
||||
if str.len() < 9 {
|
||||
Err("less than 9 characters long")?
|
||||
}
|
||||
if !matches_char(str, 2, ':') || !matches_char(str, 5, ':') {
|
||||
Err("missing colon in correct place")?
|
||||
}
|
||||
|
||||
// parse hh:mm:ss
|
||||
if !str.is_char_boundary(8) {
|
||||
Err("contains non-ascii char")?
|
||||
}
|
||||
let mut hms = (str[..8])
|
||||
.splitn(3, ':')
|
||||
.filter_map(|t| t.parse::<usize>().ok());
|
||||
let (Some(mut h), Some(mut m), Some(s)) = (hms.next(), hms.next(), hms.next()) else {
|
||||
Err("non-positive hour/min/sec")?
|
||||
};
|
||||
if h > 23 || m > 59 || s > 60 {
|
||||
Err("hour/min/sec out of range")?
|
||||
}
|
||||
str = &str[8..];
|
||||
|
||||
// parse sec-frac if present
|
||||
if let Some(rem) = str.strip_prefix('.') {
|
||||
let n_digits = rem.chars().take_while(char::is_ascii_digit).count();
|
||||
if n_digits == 0 {
|
||||
Err("no digits in second fraction")?;
|
||||
}
|
||||
str = &rem[n_digits..];
|
||||
}
|
||||
|
||||
if str != "z" && str != "Z" {
|
||||
// parse time-numoffset
|
||||
if str.len() != 6 {
|
||||
Err("offset must be 6 characters long")?;
|
||||
}
|
||||
let sign: isize = match str.chars().next() {
|
||||
Some('+') => -1,
|
||||
Some('-') => 1,
|
||||
_ => return Err("offset must begin with plus/minus")?,
|
||||
};
|
||||
str = &str[1..];
|
||||
if !matches_char(str, 2, ':') {
|
||||
Err("missing colon in offset at correct place")?
|
||||
}
|
||||
|
||||
let mut zhm = str.splitn(2, ':').filter_map(|t| t.parse::<usize>().ok());
|
||||
let (Some(zh), Some(zm)) = (zhm.next(), zhm.next()) else {
|
||||
Err("non-positive hour/min in offset")?
|
||||
};
|
||||
if zh > 23 || zm > 59 {
|
||||
Err("hour/min in offset out of range")?
|
||||
}
|
||||
|
||||
// apply timezone
|
||||
let mut hm = (h * 60 + m) as isize + sign * (zh * 60 + zm) as isize;
|
||||
if hm < 0 {
|
||||
hm += 24 * 60;
|
||||
debug_assert!(hm >= 0);
|
||||
}
|
||||
let hm = hm as usize;
|
||||
(h, m) = (hm / 60, hm % 60);
|
||||
}
|
||||
|
||||
// check leap second
|
||||
if !(s < 60 || (h == 23 && m == 59)) {
|
||||
Err("invalid leap second")?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_date_time(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
check_date_time(s)
|
||||
}
|
||||
|
||||
fn check_date_time(s: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
// min: yyyy-mm-ddThh:mm:ssZ
|
||||
if s.len() < 20 {
|
||||
Err("less than 20 characters long")?;
|
||||
}
|
||||
if !s.is_char_boundary(10) || !s[10..].starts_with(['t', 'T']) {
|
||||
Err("11th character must be t or T")?;
|
||||
}
|
||||
if let Err(e) = check_date(&s[..10]) {
|
||||
Err(format!("invalid date element: {e}"))?;
|
||||
}
|
||||
if let Err(e) = check_time(&s[11..]) {
|
||||
Err(format!("invalid time element: {e}"))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_duration(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
check_duration(s)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// see https://datatracker.ietf.org/doc/html/rfc3339#appendix-A
|
||||
fn check_duration(s: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
// must start with 'P'
|
||||
let Some(s) = s.strip_prefix('P') else {
|
||||
Err("must start with P")?
|
||||
};
|
||||
if s.is_empty() {
|
||||
Err("nothing after P")?
|
||||
}
|
||||
|
||||
// dur-week
|
||||
if let Some(s) = s.strip_suffix('W') {
|
||||
if s.is_empty() {
|
||||
Err("no number in week")?
|
||||
}
|
||||
if !s.chars().all(|c| c.is_ascii_digit()) {
|
||||
Err("invalid week")?
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
static UNITS: [&str; 2] = ["YMD", "HMS"];
|
||||
for (i, s) in s.split('T').enumerate() {
|
||||
let mut s = s;
|
||||
if i != 0 && s.is_empty() {
|
||||
Err("no time elements")?
|
||||
}
|
||||
let Some(mut units) = UNITS.get(i).cloned() else {
|
||||
Err("more than one T")?
|
||||
};
|
||||
while !s.is_empty() {
|
||||
let digit_count = s.chars().take_while(char::is_ascii_digit).count();
|
||||
if digit_count == 0 {
|
||||
Err("missing number")?
|
||||
}
|
||||
s = &s[digit_count..];
|
||||
let Some(unit) = s.chars().next() else {
|
||||
Err("missing unit")?
|
||||
};
|
||||
let Some(j) = units.find(unit) else {
|
||||
if UNITS[i].contains(unit) {
|
||||
Err(format!("unit {unit} out of order"))?
|
||||
}
|
||||
Err(format!("invalid unit {unit}"))?
|
||||
};
|
||||
units = &units[j + 1..];
|
||||
s = &s[1..];
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// see https://datatracker.ietf.org/doc/html/rfc3339#appendix-A
|
||||
fn validate_period(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let Some(slash) = s.find('/') else {
|
||||
Err("missing slash")?
|
||||
};
|
||||
|
||||
let (start, end) = (&s[..slash], &s[slash + 1..]);
|
||||
if start.starts_with('P') {
|
||||
if let Err(e) = check_duration(start) {
|
||||
Err(format!("invalid start duration: {e}"))?
|
||||
}
|
||||
if let Err(e) = check_date_time(end) {
|
||||
Err(format!("invalid end date-time: {e}"))?
|
||||
}
|
||||
} else {
|
||||
if let Err(e) = check_date_time(start) {
|
||||
Err(format!("invalid start date-time: {e}"))?
|
||||
}
|
||||
if end.starts_with('P') {
|
||||
if let Err(e) = check_duration(end) {
|
||||
Err(format!("invalid end duration: {e}"))?;
|
||||
}
|
||||
} else if let Err(e) = check_date_time(end) {
|
||||
Err(format!("invalid end date-time: {e}"))?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_hostname(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
check_hostname(s)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// see https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
|
||||
fn check_hostname(s: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
// entire hostname (including the delimiting dots but not a trailing dot) has a maximum of 253 ASCII characters
|
||||
|
||||
if s.len() > 253 {
|
||||
Err("more than 253 characters long")?
|
||||
}
|
||||
|
||||
// Hostnames are composed of series of labels concatenated with dots, as are all domain names
|
||||
for label in s.split('.') {
|
||||
// Each label must be from 1 to 63 characters long
|
||||
if !matches!(label.len(), 1..=63) {
|
||||
Err("label must be 1 to 63 characters long")?;
|
||||
}
|
||||
|
||||
// labels must not start or end with a hyphen
|
||||
if label.starts_with('-') {
|
||||
Err("label starts with hyphen")?;
|
||||
}
|
||||
|
||||
if label.ends_with('-') {
|
||||
Err("label ends with hyphen")?;
|
||||
}
|
||||
|
||||
// labels may contain only the ASCII letters 'a' through 'z' (in a case-insensitive manner),
|
||||
// the digits '0' through '9', and the hyphen ('-')
|
||||
if let Some(ch) = label
|
||||
.chars()
|
||||
.find(|c| !matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '-'))
|
||||
{
|
||||
Err(format!("invalid character {ch:?}"))?;
|
||||
}
|
||||
|
||||
// labels must not contain "--" in 3rd and 4th position unless they start with "xn--"
|
||||
if label.len() >= 4 && &label[2..4] == "--" {
|
||||
if !label.starts_with("xn--") {
|
||||
Err("label has -- in 3rd/4th position but does not start with xn--")?;
|
||||
} else {
|
||||
let (unicode, errors) = idna::domain_to_unicode(label);
|
||||
if let Err(_) = errors {
|
||||
Err("invalid punycode")?;
|
||||
}
|
||||
check_unicode_idn_constraints(&unicode).map_err(|e| format!("invalid punycode/IDN: {e}"))?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_idn_hostname(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
check_idn_hostname(s)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
static DISALLOWED: [char; 10] = [
|
||||
'\u{0640}', // ARABIC TATWEEL
|
||||
'\u{07FA}', // NKO LAJANYALAN
|
||||
'\u{302E}', // HANGUL SINGLE DOT TONE MARK
|
||||
'\u{302F}', // HANGUL DOUBLE DOT TONE MARK
|
||||
'\u{3031}', // VERTICAL KANA REPEAT MARK
|
||||
'\u{3032}', // VERTICAL KANA REPEAT WITH VOICED SOUND MARK
|
||||
'\u{3033}', // VERTICAL KANA REPEAT MARK UPPER HALF
|
||||
'\u{3034}', // VERTICAL KANA REPEAT WITH VOICED SOUND MARK UPPER HA
|
||||
'\u{3035}', // VERTICAL KANA REPEAT MARK LOWER HALF
|
||||
'\u{303B}', // VERTICAL IDEOGRAPHIC ITERATION MARK
|
||||
];
|
||||
|
||||
fn check_idn_hostname(s: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let s = idna::domain_to_ascii_strict(s).map_err(|e| format!("idna error: {:?}", e))?;
|
||||
let (unicode, errors) = idna::domain_to_unicode(&s);
|
||||
if let Err(e) = errors {
|
||||
Err(format!("idna decoding error: {:?}", e))?;
|
||||
}
|
||||
check_unicode_idn_constraints(&unicode)?;
|
||||
check_hostname(&s)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_unicode_idn_constraints(unicode: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
// see https://www.rfc-editor.org/rfc/rfc5892#section-2.6
|
||||
{
|
||||
if unicode.contains(DISALLOWED) {
|
||||
Err("contains disallowed character")?;
|
||||
}
|
||||
}
|
||||
|
||||
// unicode string must not contain "--" in 3rd and 4th position
|
||||
// and must not start and end with a '-'
|
||||
// see https://www.rfc-editor.org/rfc/rfc5891#section-4.2.3.1
|
||||
{
|
||||
let count: usize = unicode
|
||||
.chars()
|
||||
.skip(2)
|
||||
.take(2)
|
||||
.map(|c| if c == '-' { 1 } else { 0 })
|
||||
.sum();
|
||||
if count == 2 {
|
||||
Err("unicode string must not contain '--' in 3rd and 4th position")?;
|
||||
}
|
||||
}
|
||||
|
||||
// MIDDLE DOT is allowed between 'l' characters only
|
||||
// see https://www.rfc-editor.org/rfc/rfc5892#appendix-A.3
|
||||
{
|
||||
let middle_dot = '\u{00b7}';
|
||||
let mut s = unicode;
|
||||
while let Some(i) = s.find(middle_dot) {
|
||||
let prefix = &s[..i];
|
||||
let suffix = &s[i + middle_dot.len_utf8()..];
|
||||
if !prefix.ends_with('l') || !suffix.ends_with('l') {
|
||||
Err("MIDDLE DOT is allowed between 'l' characters only")?;
|
||||
}
|
||||
s = suffix;
|
||||
}
|
||||
}
|
||||
|
||||
// Greek KERAIA must be followed by Greek character
|
||||
// see https://www.rfc-editor.org/rfc/rfc5892#appendix-A.4
|
||||
{
|
||||
let keralia = '\u{0375}';
|
||||
let greek = '\u{0370}'..='\u{03FF}';
|
||||
let mut s = unicode;
|
||||
while let Some(i) = s.find(keralia) {
|
||||
let suffix = &s[i + keralia.len_utf8()..];
|
||||
if !suffix.starts_with(|c| greek.contains(&c)) {
|
||||
Err("Greek KERAIA must be followed by Greek character")?;
|
||||
}
|
||||
s = suffix;
|
||||
}
|
||||
}
|
||||
|
||||
// Hebrew GERESH must be preceded by Hebrew character
|
||||
// see https://www.rfc-editor.org/rfc/rfc5892#appendix-A.5
|
||||
//
|
||||
// Hebrew GERSHAYIM must be preceded by Hebrew character
|
||||
// see https://www.rfc-editor.org/rfc/rfc5892#appendix-A.6
|
||||
{
|
||||
let geresh = '\u{05F3}';
|
||||
let gereshayim = '\u{05F4}';
|
||||
let hebrew = '\u{0590}'..='\u{05FF}';
|
||||
for ch in [geresh, gereshayim] {
|
||||
let mut s = unicode;
|
||||
while let Some(i) = s.find(ch) {
|
||||
let prefix = &s[..i];
|
||||
if !prefix.ends_with(|c| hebrew.contains(&c)) {
|
||||
if i == 0 {
|
||||
Err("Hebrew GERESH must be preceded by Hebrew character")?;
|
||||
} else {
|
||||
Err("Hebrew GERESHYIM must be preceded by Hebrew character")?;
|
||||
}
|
||||
}
|
||||
let suffix = &s[i + ch.len_utf8()..];
|
||||
s = suffix;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// KATAKANA MIDDLE DOT must be with Hiragana, Katakana, or Han
|
||||
// see https://www.rfc-editor.org/rfc/rfc5892#appendix-A.7
|
||||
{
|
||||
let katakana_middle_dot = '\u{30FB}';
|
||||
if unicode.contains(katakana_middle_dot) {
|
||||
let hiragana = '\u{3040}'..='\u{309F}';
|
||||
let katakana = '\u{30A0}'..='\u{30FF}';
|
||||
let han = '\u{4E00}'..='\u{9FFF}'; // https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block): is this range correct??
|
||||
if unicode.contains(|c| hiragana.contains(&c))
|
||||
|| unicode.contains(|c| c != katakana_middle_dot && katakana.contains(&c))
|
||||
|| unicode.contains(|c| han.contains(&c))
|
||||
{
|
||||
// ok
|
||||
} else {
|
||||
Err("KATAKANA MIDDLE DOT must be with Hiragana, Katakana, or Han")?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ARABIC-INDIC DIGITS and Extended Arabic-Indic Digits cannot be mixed
|
||||
// see https://www.rfc-editor.org/rfc/rfc5892#appendix-A.8
|
||||
// see https://www.rfc-editor.org/rfc/rfc5892#appendix-A.9
|
||||
{
|
||||
let arabic_indic_digits = '\u{0660}'..='\u{0669}';
|
||||
let extended_arabic_indic_digits = '\u{06F0}'..='\u{06F9}';
|
||||
if unicode.contains(|c| arabic_indic_digits.contains(&c))
|
||||
&& unicode.contains(|c| extended_arabic_indic_digits.contains(&c))
|
||||
{
|
||||
Err("ARABIC-INDIC DIGITS and Extended Arabic-Indic Digits cannot be mixed")?;
|
||||
}
|
||||
}
|
||||
|
||||
// ZERO WIDTH JOINER must be preceded by Virama
|
||||
// see https://www.rfc-editor.org/rfc/rfc5892#appendix-A.2
|
||||
{
|
||||
let zero_width_jointer = '\u{200D}';
|
||||
static VIRAMA: [char; 61] = [
|
||||
'\u{094D}',
|
||||
'\u{09CD}',
|
||||
'\u{0A4D}',
|
||||
'\u{0ACD}',
|
||||
'\u{0B4D}',
|
||||
'\u{0BCD}',
|
||||
'\u{0C4D}',
|
||||
'\u{0CCD}',
|
||||
'\u{0D3B}',
|
||||
'\u{0D3C}',
|
||||
'\u{0D4D}',
|
||||
'\u{0DCA}',
|
||||
'\u{0E3A}',
|
||||
'\u{0EBA}',
|
||||
'\u{0F84}',
|
||||
'\u{1039}',
|
||||
'\u{103A}',
|
||||
'\u{1714}',
|
||||
'\u{1734}',
|
||||
'\u{17D2}',
|
||||
'\u{1A60}',
|
||||
'\u{1B44}',
|
||||
'\u{1BAA}',
|
||||
'\u{1BAB}',
|
||||
'\u{1BF2}',
|
||||
'\u{1BF3}',
|
||||
'\u{2D7F}',
|
||||
'\u{A806}',
|
||||
'\u{A82C}',
|
||||
'\u{A8C4}',
|
||||
'\u{A953}',
|
||||
'\u{A9C0}',
|
||||
'\u{AAF6}',
|
||||
'\u{ABED}',
|
||||
'\u{10A3F}',
|
||||
'\u{11046}',
|
||||
'\u{1107F}',
|
||||
'\u{110B9}',
|
||||
'\u{11133}',
|
||||
'\u{11134}',
|
||||
'\u{111C0}',
|
||||
'\u{11235}',
|
||||
'\u{112EA}',
|
||||
'\u{1134D}',
|
||||
'\u{11442}',
|
||||
'\u{114C2}',
|
||||
'\u{115BF}',
|
||||
'\u{1163F}',
|
||||
'\u{116B6}',
|
||||
'\u{1172B}',
|
||||
'\u{11839}',
|
||||
'\u{1193D}',
|
||||
'\u{1193E}',
|
||||
'\u{119E0}',
|
||||
'\u{11A34}',
|
||||
'\u{11A47}',
|
||||
'\u{11A99}',
|
||||
'\u{11C3F}',
|
||||
'\u{11D44}',
|
||||
'\u{11D45}',
|
||||
'\u{11D97}',
|
||||
]; // https://www.compart.com/en/unicode/combining/9
|
||||
let mut s = unicode;
|
||||
while let Some(i) = s.find(zero_width_jointer) {
|
||||
let prefix = &s[..i];
|
||||
if !prefix.ends_with(VIRAMA) {
|
||||
Err("ZERO WIDTH JOINER must be preceded by Virama")?;
|
||||
}
|
||||
let suffix = &s[i + zero_width_jointer.len_utf8()..];
|
||||
s = suffix;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_email(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
check_email(s)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// see https://en.wikipedia.org/wiki/Email_address
|
||||
fn check_email(s: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
// entire email address to be no more than 254 characters long
|
||||
if s.len() > 254 {
|
||||
Err("more than 254 characters long")?
|
||||
}
|
||||
|
||||
// email address is generally recognized as having two parts joined with an at-sign
|
||||
let Some(at) = s.rfind('@') else {
|
||||
Err("missing @")?
|
||||
};
|
||||
let (local, domain) = (&s[..at], &s[at + 1..]);
|
||||
|
||||
// local part may be up to 64 characters long
|
||||
if local.len() > 64 {
|
||||
Err("local part more than 64 characters long")?
|
||||
}
|
||||
|
||||
if local.len() > 1 && local.starts_with('"') && local.ends_with('"') {
|
||||
// quoted
|
||||
let local = &local[1..local.len() - 1];
|
||||
if local.contains(['\\', '"']) {
|
||||
Err("backslash and quote not allowed within quoted local part")?
|
||||
}
|
||||
} else {
|
||||
// unquoted
|
||||
|
||||
if local.starts_with('.') {
|
||||
Err("starts with dot")?
|
||||
}
|
||||
if local.ends_with('.') {
|
||||
Err("ends with dot")?
|
||||
}
|
||||
|
||||
// consecutive dots not allowed
|
||||
if local.contains("..") {
|
||||
Err("consecutive dots")?
|
||||
}
|
||||
|
||||
// check allowd chars
|
||||
if let Some(ch) = local
|
||||
.chars()
|
||||
.find(|c| !(c.is_ascii_alphanumeric() || ".!#$%&'*+-/=?^_`{|}~".contains(*c)))
|
||||
{
|
||||
Err(format!("invalid character {ch:?}"))?
|
||||
}
|
||||
}
|
||||
|
||||
// domain if enclosed in brackets, must match an IP address
|
||||
if domain.starts_with('[') && domain.ends_with(']') {
|
||||
let s = &domain[1..domain.len() - 1];
|
||||
if let Some(s) = s.strip_prefix("IPv6:") {
|
||||
if let Err(e) = s.parse::<Ipv6Addr>() {
|
||||
Err(format!("invalid ipv6 address: {e}"))?
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if let Err(e) = s.parse::<Ipv4Addr>() {
|
||||
Err(format!("invalid ipv4 address: {e}"))?
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// domain must match the requirements for a hostname
|
||||
if let Err(e) = check_hostname(domain) {
|
||||
Err(format!("invalid domain: {e}"))?
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_idn_email(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let Some(at) = s.rfind('@') else {
|
||||
Err("missing @")?
|
||||
};
|
||||
let (local, domain) = (&s[..at], &s[at + 1..]);
|
||||
|
||||
let local = idna::domain_to_ascii_strict(local).map_err(|e| format!("idna error: {:?}", e))?;
|
||||
let domain = idna::domain_to_ascii_strict(domain).map_err(|e| format!("idna error: {:?}", e))?;
|
||||
if let Err(e) = check_idn_hostname(&domain) {
|
||||
Err(format!("invalid domain: {e}"))?
|
||||
}
|
||||
check_email(&format!("{local}@{domain}"))
|
||||
}
|
||||
|
||||
fn validate_json_pointer(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
check_json_pointer(s)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// see https://www.rfc-editor.org/rfc/rfc6901#section-3
|
||||
fn check_json_pointer(s: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
if s.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
if !s.starts_with('/') {
|
||||
Err("not starting with slash")?;
|
||||
}
|
||||
for token in s.split('/').skip(1) {
|
||||
let mut chars = token.chars();
|
||||
while let Some(ch) = chars.next() {
|
||||
if ch == '~' {
|
||||
if !matches!(chars.next(), Some('0' | '1')) {
|
||||
Err("~ must be followed by 0 or 1")?;
|
||||
}
|
||||
} else if !matches!(ch, '\x00'..='\x2E' | '\x30'..='\x7D' | '\x7F'..='\u{10FFFF}') {
|
||||
Err("contains disallowed character")?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// see https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01#section-3
|
||||
fn validate_relative_json_pointer(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
// start with non-negative-integer
|
||||
let num_digits = s.chars().take_while(char::is_ascii_digit).count();
|
||||
if num_digits == 0 {
|
||||
Err("must start with non-negative integer")?;
|
||||
}
|
||||
if num_digits > 1 && s.starts_with('0') {
|
||||
Err("starts with zero")?;
|
||||
}
|
||||
let s = &s[num_digits..];
|
||||
|
||||
// followed by either json-pointer or '#'
|
||||
if s == "#" {
|
||||
return Ok(());
|
||||
}
|
||||
if let Err(e) = check_json_pointer(s) {
|
||||
Err(format!("invalid json-pointer element: {e}"))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// see https://datatracker.ietf.org/doc/html/rfc4122#page-4
|
||||
fn validate_uuid(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
static HEX_GROUPS: [usize; 5] = [8, 4, 4, 4, 12];
|
||||
let mut i = 0;
|
||||
for group in s.split('-') {
|
||||
if i >= HEX_GROUPS.len() {
|
||||
Err("more than 5 elements")?;
|
||||
}
|
||||
if group.len() != HEX_GROUPS[i] {
|
||||
Err(format!(
|
||||
"element {} must be {} characters long",
|
||||
i + 1,
|
||||
HEX_GROUPS[i]
|
||||
))?;
|
||||
}
|
||||
if let Some(ch) = group.chars().find(|c| !c.is_ascii_hexdigit()) {
|
||||
Err(format!("non-hex character {ch:?}"))?;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
if i != HEX_GROUPS.len() {
|
||||
Err("must have 5 elements")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_uri(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
if fluent_uri::UriRef::parse(s.as_str()).map_err(|e| e.to_string())?.scheme().is_none() {
|
||||
Err("relative url")?;
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_iri(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
match Url::parse(s) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(url::ParseError::RelativeUrlWithoutBase) => Err("relative url")?,
|
||||
Err(e) => Err(e)?,
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref TEMP_URL: Url = Url::parse("http://temp.com").unwrap();
|
||||
}
|
||||
|
||||
fn parse_uri_reference(s: &str) -> Result<Url, Box<dyn Error + Send + Sync>> {
|
||||
if s.contains('\\') {
|
||||
Err("contains \\\\")?;
|
||||
}
|
||||
Ok(TEMP_URL.join(s)?)
|
||||
}
|
||||
|
||||
fn validate_uri_reference(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
fluent_uri::UriRef::parse(s.as_str()).map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_iri_reference(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
parse_uri_reference(s)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_uri_template(v: &Value) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let Value::String(s) = v else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let url = parse_uri_reference(s)?;
|
||||
|
||||
let path = url.path();
|
||||
// path we got has curly bases percent encoded
|
||||
let path = percent_decode_str(path).decode_utf8()?;
|
||||
|
||||
// ensure curly brackets are not nested and balanced
|
||||
for part in path.as_ref().split('/') {
|
||||
let mut want = true;
|
||||
for got in part
|
||||
.chars()
|
||||
.filter(|c| matches!(c, '{' | '}'))
|
||||
.map(|c| c == '{')
|
||||
{
|
||||
if got != want {
|
||||
Err("nested curly braces")?;
|
||||
}
|
||||
want = !want;
|
||||
}
|
||||
if !want {
|
||||
Err("no matching closing brace")?
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
911
src/lib.rs
911
src/lib.rs
@ -2,859 +2,144 @@ use pgrx::*;
|
||||
|
||||
pg_module_magic!();
|
||||
|
||||
use boon::{CompileError, Compiler, ErrorKind, SchemaIndex, Schemas, ValidationError, Type, Types, ValidationOptions};
|
||||
use lazy_static::lazy_static;
|
||||
use serde_json::{json, Value, Number};
|
||||
use std::borrow::Cow;
|
||||
use std::collections::hash_map::Entry;
|
||||
use std::{collections::{HashMap, HashSet}, sync::RwLock};
|
||||
pub mod compiler;
|
||||
pub mod drop;
|
||||
pub mod formats;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
enum SchemaType {
|
||||
Enum,
|
||||
Type,
|
||||
Family, // Added for generated hierarchy schemas
|
||||
PublicPunc,
|
||||
PrivatePunc,
|
||||
}
|
||||
pub mod registry;
|
||||
mod schema;
|
||||
pub mod util;
|
||||
mod validator;
|
||||
|
||||
struct Schema {
|
||||
index: SchemaIndex,
|
||||
t: SchemaType,
|
||||
}
|
||||
|
||||
struct Cache {
|
||||
schemas: Schemas,
|
||||
map: HashMap<String, Schema>,
|
||||
}
|
||||
|
||||
// Structure to hold error information without lifetimes
|
||||
#[derive(Debug)]
|
||||
struct Error {
|
||||
path: String,
|
||||
code: String,
|
||||
message: String,
|
||||
cause: Value, // Changed from String to Value to store JSON
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref SCHEMA_CACHE: RwLock<Cache> = RwLock::new(Cache {
|
||||
schemas: Schemas::new(),
|
||||
map: HashMap::new(),
|
||||
});
|
||||
}
|
||||
use crate::registry::REGISTRY;
|
||||
use crate::schema::Schema;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
#[pg_extern(strict)]
|
||||
fn cache_json_schemas(enums: JsonB, types: JsonB, puncs: JsonB) -> JsonB {
|
||||
let mut cache = SCHEMA_CACHE.write().unwrap();
|
||||
let enums_value: Value = enums.0;
|
||||
let types_value: Value = types.0;
|
||||
let puncs_value: Value = puncs.0;
|
||||
let mut registry = REGISTRY.write().unwrap();
|
||||
registry.clear();
|
||||
|
||||
*cache = Cache {
|
||||
schemas: Schemas::new(),
|
||||
map: HashMap::new(),
|
||||
};
|
||||
|
||||
let mut compiler = Compiler::new();
|
||||
compiler.enable_format_assertions();
|
||||
|
||||
let mut errors = Vec::new();
|
||||
let mut schemas_to_compile = Vec::new();
|
||||
|
||||
// Phase 1: Enums
|
||||
if let Some(enums_array) = enums_value.as_array() {
|
||||
for enum_row in enums_array {
|
||||
if let Some(schemas_raw) = enum_row.get("schemas") {
|
||||
if let Some(schemas_array) = schemas_raw.as_array() {
|
||||
for schema_def in schemas_array {
|
||||
if let Some(schema_id) = schema_def.get("$id").and_then(|v| v.as_str()) {
|
||||
schemas_to_compile.push((schema_id.to_string(), schema_def.clone(), SchemaType::Enum));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 2: Types & Hierarchy Pre-processing
|
||||
let mut hierarchy_map: HashMap<String, HashSet<String>> = HashMap::new();
|
||||
if let Some(types_array) = types_value.as_array() {
|
||||
for type_row in types_array {
|
||||
// Process main schemas for the type
|
||||
if let Some(schemas_raw) = type_row.get("schemas") {
|
||||
if let Some(schemas_array) = schemas_raw.as_array() {
|
||||
for schema_def in schemas_array {
|
||||
if let Some(schema_id) = schema_def.get("$id").and_then(|v| v.as_str()) {
|
||||
schemas_to_compile.push((schema_id.to_string(), schema_def.clone(), SchemaType::Type));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process hierarchy to build .family enums
|
||||
if let Some(type_name) = type_row.get("name").and_then(|v| v.as_str()) {
|
||||
if let Some(hierarchy_raw) = type_row.get("hierarchy") {
|
||||
if let Some(hierarchy_array) = hierarchy_raw.as_array() {
|
||||
for ancestor_val in hierarchy_array {
|
||||
if let Some(ancestor_name) = ancestor_val.as_str() {
|
||||
hierarchy_map
|
||||
.entry(ancestor_name.to_string())
|
||||
// Generate Family Schemas from Types
|
||||
{
|
||||
let mut family_map: std::collections::HashMap<String, std::collections::HashSet<String>> =
|
||||
std::collections::HashMap::new();
|
||||
if let Value::Array(arr) = &types.0 {
|
||||
for item in arr {
|
||||
if let Some(name) = item.get("name").and_then(|v| v.as_str()) {
|
||||
if let Some(hierarchy) = item.get("hierarchy").and_then(|v| v.as_array()) {
|
||||
for ancestor in hierarchy {
|
||||
if let Some(anc_str) = ancestor.as_str() {
|
||||
family_map
|
||||
.entry(anc_str.to_string())
|
||||
.or_default()
|
||||
.insert(type_name.to_string());
|
||||
.insert(name.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate and add the .family schemas
|
||||
for (base_type, descendant_types) in hierarchy_map {
|
||||
let family_schema_id = format!("{}.family", base_type);
|
||||
let enum_values: Vec<String> = descendant_types.into_iter().collect();
|
||||
let family_schema = json!({
|
||||
"$id": family_schema_id,
|
||||
"type": "string",
|
||||
"enum": enum_values
|
||||
});
|
||||
schemas_to_compile.push((family_schema_id, family_schema, SchemaType::Family));
|
||||
}
|
||||
for (family_name, members) in family_map {
|
||||
let id = format!("{}.family", family_name);
|
||||
|
||||
// Phase 3: Puncs
|
||||
if let Some(puncs_array) = puncs_value.as_array() {
|
||||
for punc_row in puncs_array {
|
||||
if let Some(punc_obj) = punc_row.as_object() {
|
||||
if let Some(punc_name) = punc_obj.get("name").and_then(|v| v.as_str()) {
|
||||
let is_public = punc_obj.get("public").and_then(|v| v.as_bool()).unwrap_or(false);
|
||||
let punc_schema_type = if is_public { SchemaType::PublicPunc } else { SchemaType::PrivatePunc };
|
||||
if let Some(schemas_raw) = punc_obj.get("schemas") {
|
||||
if let Some(schemas_array) = schemas_raw.as_array() {
|
||||
for schema_def in schemas_array {
|
||||
if let Some(schema_id) = schema_def.get("$id").and_then(|v| v.as_str()) {
|
||||
let request_schema_id = format!("{}.request", punc_name);
|
||||
let response_schema_id = format!("{}.response", punc_name);
|
||||
let schema_type_for_def = if schema_id == request_schema_id || schema_id == response_schema_id {
|
||||
punc_schema_type
|
||||
} else {
|
||||
SchemaType::Type
|
||||
};
|
||||
schemas_to_compile.push((schema_id.to_string(), schema_def.clone(), schema_type_for_def));
|
||||
// Object Union (for polymorphic object validation)
|
||||
// This allows the schema to match ANY of the types in the family hierarchy
|
||||
let object_refs: Vec<Value> = members.iter().map(|s| json!({ "$ref": s })).collect();
|
||||
|
||||
let schema_json = json!({
|
||||
"$id": id,
|
||||
"oneOf": object_refs
|
||||
});
|
||||
|
||||
if let Ok(schema) = serde_json::from_value::<Schema>(schema_json) {
|
||||
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone()));
|
||||
registry.insert(id, compiled);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper to parse and cache a list of items
|
||||
let mut cache_items = |items: JsonB| {
|
||||
if let Value::Array(arr) = items.0 {
|
||||
for item in arr {
|
||||
// For now, we assume the item structure matches what the generator expects
|
||||
// or what `json_schemas.sql` sends.
|
||||
// The `Schema` struct in `schema.rs` is designed to deserialize standard JSON Schema.
|
||||
// However, the input here is an array of objects that *contain* a `schemas` array.
|
||||
// We need to extract those inner schemas.
|
||||
|
||||
if let Some(schemas_val) = item.get("schemas") {
|
||||
if let Value::Array(schemas) = schemas_val {
|
||||
for schema_val in schemas {
|
||||
// Deserialize into our robust Schema struct to ensure validity/parsing
|
||||
if let Ok(schema) = serde_json::from_value::<Schema>(schema_val.clone()) {
|
||||
if let Some(id) = &schema.obj.id {
|
||||
let id_clone = id.clone();
|
||||
// Store the compiled Schema in the registry.
|
||||
// The registry.insert method now handles simple insertion of CompiledSchema
|
||||
let compiled =
|
||||
crate::compiler::Compiler::compile(schema, Some(id_clone.clone()));
|
||||
registry.insert(id_clone, compiled);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add all resources to compiler first
|
||||
for (id, value, schema_type) in &schemas_to_compile {
|
||||
add_schema_resource(&mut compiler, id, value.clone(), *schema_type, &mut errors);
|
||||
}
|
||||
|
||||
if !errors.is_empty() {
|
||||
return JsonB(json!({ "errors": errors }));
|
||||
}
|
||||
|
||||
// Compile all schemas
|
||||
compile_all_schemas(&mut compiler, &mut cache, &schemas_to_compile, &mut errors);
|
||||
|
||||
if errors.is_empty() {
|
||||
JsonB(json!({ "response": "success" }))
|
||||
} else {
|
||||
JsonB(json!({ "errors": errors }))
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to add a schema resource (without compiling)
|
||||
fn add_schema_resource(
|
||||
compiler: &mut Compiler,
|
||||
schema_id: &str,
|
||||
schema_value: Value,
|
||||
_schema_type: SchemaType,
|
||||
errors: &mut Vec<Value>
|
||||
) {
|
||||
if let Err(e) = compiler.add_resource(schema_id, schema_value) {
|
||||
errors.push(json!({
|
||||
"code": "SCHEMA_RESOURCE_FAILED",
|
||||
"message": format!("Failed to add schema resource '{}'", schema_id),
|
||||
"details": { "schema": schema_id, "cause": format!("{}", e) }
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to compile all added resources
|
||||
fn compile_all_schemas(
|
||||
compiler: &mut Compiler,
|
||||
cache: &mut Cache,
|
||||
schemas_to_compile: &[(String, Value, SchemaType)],
|
||||
errors: &mut Vec<Value>,
|
||||
) {
|
||||
for (id, value, schema_type) in schemas_to_compile {
|
||||
match compiler.compile(id, &mut cache.schemas) {
|
||||
Ok(index) => {
|
||||
cache.map.insert(id.clone(), Schema { index, t: *schema_type });
|
||||
}
|
||||
Err(e) => {
|
||||
match &e {
|
||||
CompileError::ValidationError { src, .. } => {
|
||||
let mut error_list = Vec::new();
|
||||
collect_errors(src, &mut error_list);
|
||||
let formatted_errors = format_errors(error_list, value, id);
|
||||
errors.extend(formatted_errors);
|
||||
}
|
||||
_ => {
|
||||
errors.push(json!({
|
||||
"code": "SCHEMA_COMPILATION_FAILED",
|
||||
"message": format!("Schema '{}' compilation failed", id),
|
||||
"details": { "schema": id, "cause": format!("{:?}", e) }
|
||||
}));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
cache_items(enums);
|
||||
cache_items(types);
|
||||
cache_items(puncs); // public/private distinction logic to come later
|
||||
}
|
||||
JsonB(json!({ "response": "success" }))
|
||||
}
|
||||
|
||||
#[pg_extern(strict, parallel_safe)]
|
||||
fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||
let cache = SCHEMA_CACHE.read().unwrap();
|
||||
match cache.map.get(schema_id) {
|
||||
None => JsonB(json!({
|
||||
"errors": [{
|
||||
"code": "SCHEMA_NOT_FOUND",
|
||||
"message": format!("Schema '{}' not found in cache", schema_id),
|
||||
"details": {
|
||||
"schema": schema_id,
|
||||
"cause": "Schema was not found in bulk cache - ensure cache_json_schemas was called"
|
||||
}
|
||||
}]
|
||||
})),
|
||||
Some(schema) => {
|
||||
let instance_value: Value = instance.0;
|
||||
let options = match schema.t {
|
||||
SchemaType::PublicPunc => Some(ValidationOptions { be_strict: true }),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
match cache.schemas.validate(&instance_value, schema.index, options) {
|
||||
Ok(_) => {
|
||||
JsonB(json!({ "response": "success" }))
|
||||
}
|
||||
Err(validation_error) => {
|
||||
let mut error_list = Vec::new();
|
||||
collect_errors(&validation_error, &mut error_list);
|
||||
let errors = format_errors(error_list, &instance_value, schema_id);
|
||||
if errors.is_empty() {
|
||||
JsonB(json!({ "response": "success" }))
|
||||
} else {
|
||||
JsonB(json!({ "errors": errors }))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recursively collects validation errors
|
||||
fn collect_errors(error: &ValidationError, errors_list: &mut Vec<Error>) {
|
||||
// Check if this is a structural error that we should skip
|
||||
let is_structural = matches!(
|
||||
&error.kind,
|
||||
ErrorKind::Group | ErrorKind::AllOf | ErrorKind::AnyOf | ErrorKind::Not | ErrorKind::OneOf(_)
|
||||
);
|
||||
|
||||
if !error.causes.is_empty() || is_structural {
|
||||
for cause in &error.causes {
|
||||
collect_errors(cause, errors_list);
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let base_path = error.instance_location.to_string();
|
||||
let errors_to_add = match &error.kind {
|
||||
ErrorKind::Type { got, want } => handle_type_error(&base_path, got, want),
|
||||
ErrorKind::Required { want } => handle_required_error(&base_path, want),
|
||||
ErrorKind::Dependency { prop, missing } => handle_dependency_error(&base_path, prop, missing, false),
|
||||
ErrorKind::DependentRequired { prop, missing } => handle_dependency_error(&base_path, prop, missing, true),
|
||||
ErrorKind::AdditionalProperties { got } => handle_additional_properties_error(&base_path, got),
|
||||
ErrorKind::Enum { want } => handle_enum_error(&base_path, want),
|
||||
ErrorKind::Const { want } => handle_const_error(&base_path, want),
|
||||
ErrorKind::MinLength { got, want } => handle_min_length_error(&base_path, *got, *want),
|
||||
ErrorKind::MaxLength { got, want } => handle_max_length_error(&base_path, *got, *want),
|
||||
ErrorKind::Pattern { got, want } => handle_pattern_error(&base_path, got, want),
|
||||
ErrorKind::Minimum { got, want } => handle_minimum_error(&base_path, got, want),
|
||||
ErrorKind::Maximum { got, want } => handle_maximum_error(&base_path, got, want),
|
||||
ErrorKind::ExclusiveMinimum { got, want } => handle_exclusive_minimum_error(&base_path, got, want),
|
||||
ErrorKind::ExclusiveMaximum { got, want } => handle_exclusive_maximum_error(&base_path, got, want),
|
||||
ErrorKind::MultipleOf { got, want } => handle_multiple_of_error(&base_path, got, want),
|
||||
ErrorKind::MinItems { got, want } => handle_min_items_error(&base_path, *got, *want),
|
||||
ErrorKind::MaxItems { got, want } => handle_max_items_error(&base_path, *got, *want),
|
||||
ErrorKind::UniqueItems { got } => handle_unique_items_error(&base_path, got),
|
||||
ErrorKind::MinProperties { got, want } => handle_min_properties_error(&base_path, *got, *want),
|
||||
ErrorKind::MaxProperties { got, want } => handle_max_properties_error(&base_path, *got, *want),
|
||||
ErrorKind::AdditionalItems { got } => handle_additional_items_error(&base_path, *got),
|
||||
ErrorKind::Format { want, got, err } => handle_format_error(&base_path, want, got, err),
|
||||
ErrorKind::PropertyName { prop } => handle_property_name_error(&base_path, prop),
|
||||
ErrorKind::Contains => handle_contains_error(&base_path),
|
||||
ErrorKind::MinContains { got, want } => handle_min_contains_error(&base_path, got, *want),
|
||||
ErrorKind::MaxContains { got, want } => handle_max_contains_error(&base_path, got, *want),
|
||||
ErrorKind::ContentEncoding { want, err } => handle_content_encoding_error(&base_path, want, err),
|
||||
ErrorKind::ContentMediaType { want, err, .. } => handle_content_media_type_error(&base_path, want, err),
|
||||
ErrorKind::FalseSchema => handle_false_schema_error(&base_path),
|
||||
ErrorKind::Not => handle_not_error(&base_path),
|
||||
ErrorKind::RefCycle { url, kw_loc1, kw_loc2 } => handle_ref_cycle_error(&base_path, url, kw_loc1, kw_loc2),
|
||||
ErrorKind::Reference { kw, url } => handle_reference_error(&base_path, kw, url),
|
||||
ErrorKind::Schema { url } => handle_schema_error(&base_path, url),
|
||||
ErrorKind::ContentSchema => handle_content_schema_error(&base_path),
|
||||
ErrorKind::Group => handle_group_error(&base_path),
|
||||
ErrorKind::AllOf => handle_all_of_error(&base_path),
|
||||
ErrorKind::AnyOf => handle_any_of_error(&base_path),
|
||||
ErrorKind::OneOf(matched) => handle_one_of_error(&base_path, matched),
|
||||
};
|
||||
|
||||
errors_list.extend(errors_to_add);
|
||||
}
|
||||
|
||||
// Handler functions for each error kind
|
||||
fn handle_type_error(base_path: &str, got: &Type, want: &Types) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "TYPE_MISMATCH".to_string(),
|
||||
message: format!("Expected {} but got {}",
|
||||
want.iter().map(|t| t.to_string()).collect::<Vec<_>>().join(" or "),
|
||||
got
|
||||
),
|
||||
cause: json!({
|
||||
"got": got.to_string(),
|
||||
"want": want.iter().map(|t| t.to_string()).collect::<Vec<_>>()
|
||||
}),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_required_error(base_path: &str, want: &[&str]) -> Vec<Error> {
|
||||
// Create a separate error for each missing required field
|
||||
want.iter().map(|missing_field| {
|
||||
let field_path = if base_path.is_empty() {
|
||||
format!("/{}", missing_field)
|
||||
} else {
|
||||
format!("{}/{}", base_path, missing_field)
|
||||
};
|
||||
|
||||
Error {
|
||||
path: field_path,
|
||||
code: "REQUIRED_FIELD_MISSING".to_string(),
|
||||
message: format!("Required field '{}' is missing", missing_field),
|
||||
cause: json!({ "want": [missing_field] }),
|
||||
}
|
||||
}).collect()
|
||||
}
|
||||
|
||||
fn handle_dependency_error(base_path: &str, prop: &str, missing: &[&str], is_dependent_required: bool) -> Vec<Error> {
|
||||
// Create a separate error for each missing field
|
||||
missing.iter().map(|missing_field| {
|
||||
let field_path = if base_path.is_empty() {
|
||||
format!("/{}", missing_field)
|
||||
} else {
|
||||
format!("{}/{}", base_path, missing_field)
|
||||
};
|
||||
|
||||
let (code, message) = if is_dependent_required {
|
||||
(
|
||||
"DEPENDENT_REQUIRED_MISSING".to_string(),
|
||||
format!("Field '{}' is required when '{}' is present", missing_field, prop),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
"DEPENDENCY_FAILED".to_string(),
|
||||
format!("Field '{}' is required when '{}' is present", missing_field, prop),
|
||||
)
|
||||
};
|
||||
|
||||
Error {
|
||||
path: field_path,
|
||||
code,
|
||||
message,
|
||||
cause: json!({ "prop": prop, "missing": [missing_field] }),
|
||||
}
|
||||
}).collect()
|
||||
}
|
||||
|
||||
fn handle_additional_properties_error(base_path: &str, got: &[Cow<str>]) -> Vec<Error> {
|
||||
let mut errors = Vec::new();
|
||||
for extra_prop in got {
|
||||
let field_path = if base_path.is_empty() {
|
||||
format!("/{}", extra_prop)
|
||||
} else {
|
||||
format!("{}/{}", base_path, extra_prop)
|
||||
};
|
||||
errors.push(Error {
|
||||
path: field_path,
|
||||
code: "ADDITIONAL_PROPERTIES_NOT_ALLOWED".to_string(),
|
||||
message: format!("Property '{}' is not allowed", extra_prop),
|
||||
cause: json!({ "got": [extra_prop.to_string()] }),
|
||||
});
|
||||
}
|
||||
errors
|
||||
}
|
||||
|
||||
fn handle_enum_error(base_path: &str, want: &[Value]) -> Vec<Error> {
|
||||
let message = if want.len() == 1 {
|
||||
format!("Value must be {}", serde_json::to_string(&want[0]).unwrap_or_else(|_| "unknown".to_string()))
|
||||
} else {
|
||||
format!("Value must be one of: {}",
|
||||
want.iter()
|
||||
.map(|v| serde_json::to_string(v).unwrap_or_else(|_| "unknown".to_string()))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
)
|
||||
};
|
||||
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "ENUM_VIOLATED".to_string(),
|
||||
message,
|
||||
cause: json!({ "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_const_error(base_path: &str, want: &Value) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "CONST_VIOLATED".to_string(),
|
||||
message: format!("Value must be exactly {}", serde_json::to_string(want).unwrap_or_else(|_| "unknown".to_string())),
|
||||
cause: json!({ "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_min_length_error(base_path: &str, got: usize, want: usize) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "MIN_LENGTH_VIOLATED".to_string(),
|
||||
message: format!("String length must be at least {} characters, but got {}", want, got),
|
||||
cause: json!({ "got": got, "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_max_length_error(base_path: &str, got: usize, want: usize) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "MAX_LENGTH_VIOLATED".to_string(),
|
||||
message: format!("String length must be at most {} characters, but got {}", want, got),
|
||||
cause: json!({ "got": got, "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_pattern_error(base_path: &str, got: &Cow<str>, want: &str) -> Vec<Error> {
|
||||
let display_value = if got.len() > 50 {
|
||||
format!("{}...", &got[..50])
|
||||
} else {
|
||||
got.to_string()
|
||||
};
|
||||
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "PATTERN_VIOLATED".to_string(),
|
||||
message: format!("Value '{}' does not match pattern '{}'", display_value, want),
|
||||
cause: json!({ "got": got.to_string(), "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_minimum_error(base_path: &str, got: &Cow<Number>, want: &Number) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "MINIMUM_VIOLATED".to_string(),
|
||||
message: format!("Value must be at least {}, but got {}", want, got),
|
||||
cause: json!({ "got": got, "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_maximum_error(base_path: &str, got: &Cow<Number>, want: &Number) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "MAXIMUM_VIOLATED".to_string(),
|
||||
message: format!("Value must be at most {}, but got {}", want, got),
|
||||
cause: json!({ "got": got, "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_exclusive_minimum_error(base_path: &str, got: &Cow<Number>, want: &Number) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "EXCLUSIVE_MINIMUM_VIOLATED".to_string(),
|
||||
message: format!("Value must be greater than {}, but got {}", want, got),
|
||||
cause: json!({ "got": got, "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_exclusive_maximum_error(base_path: &str, got: &Cow<Number>, want: &Number) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "EXCLUSIVE_MAXIMUM_VIOLATED".to_string(),
|
||||
message: format!("Value must be less than {}, but got {}", want, got),
|
||||
cause: json!({ "got": got, "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_multiple_of_error(base_path: &str, got: &Cow<Number>, want: &Number) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "MULTIPLE_OF_VIOLATED".to_string(),
|
||||
message: format!("{} is not a multiple of {}", got, want),
|
||||
cause: json!({ "got": got, "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_min_items_error(base_path: &str, got: usize, want: usize) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "MIN_ITEMS_VIOLATED".to_string(),
|
||||
message: format!("Array must have at least {} items, but has {}", want, got),
|
||||
cause: json!({ "got": got, "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_max_items_error(base_path: &str, got: usize, want: usize) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "MAX_ITEMS_VIOLATED".to_string(),
|
||||
message: format!("Array must have at most {} items, but has {}", want, got),
|
||||
cause: json!({ "got": got, "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_unique_items_error(base_path: &str, got: &[usize; 2]) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "UNIQUE_ITEMS_VIOLATED".to_string(),
|
||||
message: format!("Array items at positions {} and {} are duplicates", got[0], got[1]),
|
||||
cause: json!({ "got": got }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_min_properties_error(base_path: &str, got: usize, want: usize) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "MIN_PROPERTIES_VIOLATED".to_string(),
|
||||
message: format!("Object must have at least {} properties, but has {}", want, got),
|
||||
cause: json!({ "got": got, "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_max_properties_error(base_path: &str, got: usize, want: usize) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "MAX_PROPERTIES_VIOLATED".to_string(),
|
||||
message: format!("Object must have at most {} properties, but has {}", want, got),
|
||||
cause: json!({ "got": got, "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_additional_items_error(base_path: &str, got: usize) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "ADDITIONAL_ITEMS_NOT_ALLOWED".to_string(),
|
||||
message: format!("Last {} array items are not allowed", got),
|
||||
cause: json!({ "got": got }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_format_error(base_path: &str, want: &str, got: &Cow<Value>, err: &Box<dyn std::error::Error>) -> Vec<Error> {
|
||||
// If the value is an empty string, skip format validation.
|
||||
if let Value::String(s) = got.as_ref() {
|
||||
if s.is_empty() {
|
||||
return vec![];
|
||||
}
|
||||
}
|
||||
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "FORMAT_INVALID".to_string(),
|
||||
message: format!("Value {} is not a valid {} format",
|
||||
serde_json::to_string(got.as_ref()).unwrap_or_else(|_| "unknown".to_string()),
|
||||
want
|
||||
),
|
||||
cause: json!({ "got": got, "want": want, "err": err.to_string() }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_property_name_error(base_path: &str, prop: &str) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "INVALID_PROPERTY_NAME".to_string(),
|
||||
message: format!("Property name '{}' is invalid", prop),
|
||||
cause: json!({ "prop": prop }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_contains_error(base_path: &str) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "CONTAINS_FAILED".to_string(),
|
||||
message: "No array items match the required schema".to_string(),
|
||||
cause: json!({}),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_min_contains_error(base_path: &str, got: &[usize], want: usize) -> Vec<Error> {
|
||||
let message = if got.is_empty() {
|
||||
format!("At least {} array items must match the schema, but none do", want)
|
||||
} else {
|
||||
format!("At least {} array items must match the schema, but only {} do (at positions {})",
|
||||
want,
|
||||
got.len(),
|
||||
got.iter().map(|i| i.to_string()).collect::<Vec<_>>().join(", ")
|
||||
)
|
||||
};
|
||||
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "MIN_CONTAINS_VIOLATED".to_string(),
|
||||
message,
|
||||
cause: json!({ "got": got, "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_max_contains_error(base_path: &str, got: &[usize], want: usize) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "MAX_CONTAINS_VIOLATED".to_string(),
|
||||
message: format!("At most {} array items can match the schema, but {} do (at positions {})",
|
||||
want,
|
||||
got.len(),
|
||||
got.iter().map(|i| i.to_string()).collect::<Vec<_>>().join(", ")
|
||||
),
|
||||
cause: json!({ "got": got, "want": want }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_content_encoding_error(base_path: &str, want: &str, err: &Box<dyn std::error::Error>) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "CONTENT_ENCODING_INVALID".to_string(),
|
||||
message: format!("Content is not valid {} encoding: {}", want, err),
|
||||
cause: json!({ "want": want, "err": err.to_string() }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_content_media_type_error(base_path: &str, want: &str, err: &Box<dyn std::error::Error>) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "CONTENT_MEDIA_TYPE_INVALID".to_string(),
|
||||
message: format!("Content is not valid {} media type: {}", want, err),
|
||||
cause: json!({ "want": want, "err": err.to_string() }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_false_schema_error(base_path: &str) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "FALSE_SCHEMA".to_string(),
|
||||
message: "This schema always fails validation".to_string(),
|
||||
cause: json!({}),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_not_error(base_path: &str) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "NOT_VIOLATED".to_string(),
|
||||
message: "Value matches a schema that it should not match".to_string(),
|
||||
cause: json!({}),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_ref_cycle_error(base_path: &str, url: &str, kw_loc1: &str, kw_loc2: &str) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "REFERENCE_CYCLE".to_string(),
|
||||
message: format!("Reference cycle detected: both '{}' and '{}' resolve to '{}'", kw_loc1, kw_loc2, url),
|
||||
cause: json!({ "url": url, "kw_loc1": kw_loc1, "kw_loc2": kw_loc2 }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_reference_error(base_path: &str, kw: &str, url: &str) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "REFERENCE_FAILED".to_string(),
|
||||
message: format!("{} reference to '{}' failed validation", kw, url),
|
||||
cause: json!({ "kw": kw, "url": url }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_schema_error(base_path: &str, url: &str) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "SCHEMA_FAILED".to_string(),
|
||||
message: format!("Schema '{}' validation failed", url),
|
||||
cause: json!({ "url": url }),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_content_schema_error(base_path: &str) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "CONTENT_SCHEMA_FAILED".to_string(),
|
||||
message: "Content schema validation failed".to_string(),
|
||||
cause: json!({}),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_group_error(base_path: &str) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "VALIDATION_FAILED".to_string(),
|
||||
message: "Validation failed".to_string(),
|
||||
cause: json!({}),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_all_of_error(base_path: &str) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "ALL_OF_VIOLATED".to_string(),
|
||||
message: "Value does not match all required schemas".to_string(),
|
||||
cause: json!({}),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_any_of_error(base_path: &str) -> Vec<Error> {
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "ANY_OF_VIOLATED".to_string(),
|
||||
message: "Value does not match any of the allowed schemas".to_string(),
|
||||
cause: json!({}),
|
||||
}]
|
||||
}
|
||||
|
||||
fn handle_one_of_error(base_path: &str, matched: &Option<(usize, usize)>) -> Vec<Error> {
|
||||
let (message, cause) = match matched {
|
||||
None => (
|
||||
"Value must match exactly one schema, but matches none".to_string(),
|
||||
json!({ "matched_indices": null })
|
||||
),
|
||||
Some((i, j)) => (
|
||||
format!("Value must match exactly one schema, but matches schemas at positions {} and {}", i, j),
|
||||
json!({ "matched_indices": [i, j] })
|
||||
),
|
||||
};
|
||||
|
||||
vec![Error {
|
||||
path: base_path.to_string(),
|
||||
code: "ONE_OF_VIOLATED".to_string(),
|
||||
message,
|
||||
cause,
|
||||
}]
|
||||
}
|
||||
|
||||
// Formats errors according to DropError structure
|
||||
fn format_errors(errors: Vec<Error>, instance: &Value, schema_id: &str) -> Vec<Value> {
|
||||
let mut unique_errors: HashMap<String, Value> = HashMap::new();
|
||||
for error in errors {
|
||||
let error_path = error.path.clone();
|
||||
if let Entry::Vacant(entry) = unique_errors.entry(error_path.clone()) {
|
||||
let failing_value = extract_value_at_path(instance, &error.path);
|
||||
entry.insert(json!({
|
||||
"code": error.code,
|
||||
"message": error.message,
|
||||
"details": {
|
||||
"path": error.path,
|
||||
"context": failing_value,
|
||||
"cause": error.cause,
|
||||
"schema": schema_id
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
unique_errors.into_values().collect::<Vec<Value>>()
|
||||
}
|
||||
|
||||
// Helper function to extract value at a JSON pointer path
|
||||
fn extract_value_at_path(instance: &Value, path: &str) -> Value {
|
||||
let parts: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
|
||||
let mut current = instance;
|
||||
|
||||
for part in parts {
|
||||
match current {
|
||||
Value::Object(map) => {
|
||||
if let Some(value) = map.get(part) {
|
||||
current = value;
|
||||
} else {
|
||||
return Value::Null;
|
||||
}
|
||||
}
|
||||
Value::Array(arr) => {
|
||||
if let Ok(index) = part.parse::<usize>() {
|
||||
if let Some(value) = arr.get(index) {
|
||||
current = value;
|
||||
} else {
|
||||
return Value::Null;
|
||||
}
|
||||
} else {
|
||||
return Value::Null;
|
||||
}
|
||||
}
|
||||
_ => return Value::Null,
|
||||
}
|
||||
}
|
||||
|
||||
current.clone()
|
||||
let drop = validator::Validator::validate(schema_id, &instance.0);
|
||||
JsonB(serde_json::to_value(drop).unwrap())
|
||||
}
|
||||
|
||||
#[pg_extern(strict, parallel_safe)]
|
||||
fn json_schema_cached(schema_id: &str) -> bool {
|
||||
let cache = SCHEMA_CACHE.read().unwrap();
|
||||
cache.map.contains_key(schema_id)
|
||||
let registry = REGISTRY.read().unwrap();
|
||||
registry.get(schema_id).is_some()
|
||||
}
|
||||
|
||||
#[pg_extern(strict)]
|
||||
fn clear_json_schemas() -> JsonB {
|
||||
let mut cache = SCHEMA_CACHE.write().unwrap();
|
||||
*cache = Cache {
|
||||
schemas: Schemas::new(),
|
||||
map: HashMap::new(),
|
||||
};
|
||||
JsonB(json!({ "response": "success" }))
|
||||
let mut registry = REGISTRY.write().unwrap();
|
||||
registry.clear();
|
||||
JsonB(json!({ "response": "success" }))
|
||||
}
|
||||
|
||||
#[pg_extern(strict, parallel_safe)]
|
||||
fn show_json_schemas() -> JsonB {
|
||||
let cache = SCHEMA_CACHE.read().unwrap();
|
||||
let ids: Vec<String> = cache.map.keys().cloned().collect();
|
||||
JsonB(json!({ "response": ids }))
|
||||
}
|
||||
|
||||
/// This module is required by `cargo pgrx test` invocations.
|
||||
/// It must be visible at the root of your extension crate.
|
||||
#[cfg(test)]
|
||||
pub mod pg_test {
|
||||
pub fn setup(_options: Vec<&str>) {
|
||||
// perform one-off initialization when the pg_test framework starts
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn postgresql_conf_options() -> Vec<&'static str> {
|
||||
// return any postgresql.conf settings that are required for your tests
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
mod helpers {
|
||||
include!("helpers.rs");
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
mod schemas {
|
||||
include!("schemas.rs");
|
||||
let registry = REGISTRY.read().unwrap();
|
||||
// Debug dump
|
||||
// In a real scenario we might return the whole map, but for now just success
|
||||
// or maybe a list of keys
|
||||
JsonB(json!({ "response": "success", "count": registry.len() }))
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "pg_test"))]
|
||||
#[pg_schema]
|
||||
mod tests {
|
||||
include!("tests.rs");
|
||||
}
|
||||
use pgrx::prelude::*;
|
||||
include!("tests.rs");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod pg_test {
|
||||
pub fn setup(_options: Vec<&str>) {
|
||||
// perform any initialization common to all tests
|
||||
}
|
||||
|
||||
pub fn postgresql_conf_options() -> Vec<&'static str> {
|
||||
// return any postgresql.conf settings that are required for your tests
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
41
src/registry.rs
Normal file
41
src/registry.rs
Normal file
@ -0,0 +1,41 @@
|
||||
use crate::compiler::CompiledSchema; // Changed from crate::schema::Schema
|
||||
use lazy_static::lazy_static;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::RwLock;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref REGISTRY: RwLock<Registry> = RwLock::new(Registry::new());
|
||||
}
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct Registry {
|
||||
pub schemas: HashMap<String, Arc<CompiledSchema>>, // Changed from Schema
|
||||
}
|
||||
|
||||
impl Registry {
|
||||
pub fn new() -> Self {
|
||||
Registry {
|
||||
schemas: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, id: String, compiled: CompiledSchema) {
|
||||
if self.schemas.contains_key(&id) {
|
||||
panic!("Duplicate schema ID inserted into registry: '{}'", id);
|
||||
}
|
||||
self.schemas.insert(id, Arc::new(compiled));
|
||||
}
|
||||
|
||||
pub fn get(&self, id: &str) -> Option<Arc<CompiledSchema>> {
|
||||
self.schemas.get(id).cloned()
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.schemas.clear();
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.schemas.len()
|
||||
}
|
||||
}
|
||||
212
src/schema.rs
Normal file
212
src/schema.rs
Normal file
@ -0,0 +1,212 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
// Schema mirrors the Go Punc Generator's schema struct for consistency.
|
||||
// It is an order-preserving representation of a JSON Schema.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct SchemaObject {
|
||||
// Core Schema Keywords
|
||||
#[serde(rename = "$id")]
|
||||
pub id: Option<String>,
|
||||
#[serde(rename = "$ref")]
|
||||
pub ref_string: Option<String>,
|
||||
#[serde(rename = "$anchor")]
|
||||
pub anchor: Option<String>,
|
||||
#[serde(rename = "$dynamicAnchor")]
|
||||
pub dynamic_anchor: Option<String>,
|
||||
#[serde(rename = "$dynamicRef")]
|
||||
pub dynamic_ref: Option<String>,
|
||||
/*
|
||||
Note: The `Ref` field in the Go struct is a pointer populated by the linker.
|
||||
In Rust, we might handle this differently (e.g., separate lookup or Rc/Arc),
|
||||
so we omit the direct recursive `Ref` field for now and rely on `ref_string`.
|
||||
*/
|
||||
pub description: Option<String>,
|
||||
pub title: Option<String>,
|
||||
#[serde(default)] // Allow missing type
|
||||
#[serde(rename = "type")]
|
||||
pub type_: Option<SchemaTypeOrArray>, // Handles string or array of strings
|
||||
|
||||
// Object Keywords
|
||||
pub properties: Option<BTreeMap<String, Arc<Schema>>>,
|
||||
#[serde(rename = "patternProperties")]
|
||||
pub pattern_properties: Option<BTreeMap<String, Arc<Schema>>>,
|
||||
pub required: Option<Vec<String>>,
|
||||
// additionalProperties can be checks against a schema or boolean (handled by Schema wrapper)
|
||||
|
||||
// dependencies can be schema dependencies or property dependencies
|
||||
pub dependencies: Option<BTreeMap<String, Dependency>>,
|
||||
|
||||
// Definitions (for $ref resolution)
|
||||
#[serde(rename = "$defs")]
|
||||
pub defs: Option<BTreeMap<String, Arc<Schema>>>,
|
||||
#[serde(rename = "definitions")]
|
||||
pub definitions: Option<BTreeMap<String, Arc<Schema>>>,
|
||||
|
||||
// Array Keywords
|
||||
#[serde(rename = "items")]
|
||||
pub items: Option<Arc<Schema>>,
|
||||
#[serde(rename = "prefixItems")]
|
||||
pub prefix_items: Option<Vec<Arc<Schema>>>,
|
||||
|
||||
// String Validation
|
||||
#[serde(rename = "minLength")]
|
||||
pub min_length: Option<f64>,
|
||||
#[serde(rename = "maxLength")]
|
||||
pub max_length: Option<f64>,
|
||||
pub pattern: Option<String>,
|
||||
|
||||
// Array Validation
|
||||
#[serde(rename = "minItems")]
|
||||
pub min_items: Option<f64>,
|
||||
#[serde(rename = "maxItems")]
|
||||
pub max_items: Option<f64>,
|
||||
#[serde(rename = "uniqueItems")]
|
||||
pub unique_items: Option<bool>,
|
||||
#[serde(rename = "contains")]
|
||||
pub contains: Option<Arc<Schema>>,
|
||||
#[serde(rename = "minContains")]
|
||||
pub min_contains: Option<f64>,
|
||||
#[serde(rename = "maxContains")]
|
||||
pub max_contains: Option<f64>,
|
||||
|
||||
// Object Validation
|
||||
#[serde(rename = "minProperties")]
|
||||
pub min_properties: Option<f64>,
|
||||
#[serde(rename = "maxProperties")]
|
||||
pub max_properties: Option<f64>,
|
||||
#[serde(rename = "propertyNames")]
|
||||
pub property_names: Option<Arc<Schema>>,
|
||||
#[serde(rename = "dependentRequired")]
|
||||
pub dependent_required: Option<BTreeMap<String, Vec<String>>>,
|
||||
#[serde(rename = "dependentSchemas")]
|
||||
pub dependent_schemas: Option<BTreeMap<String, Arc<Schema>>>,
|
||||
|
||||
// Numeric Validation
|
||||
pub format: Option<String>,
|
||||
#[serde(rename = "enum")]
|
||||
pub enum_: Option<Vec<Value>>, // `enum` is a reserved keyword in Rust
|
||||
#[serde(default, rename = "const")]
|
||||
pub const_: Option<Value>,
|
||||
|
||||
// Numeric Validation
|
||||
#[serde(rename = "multipleOf")]
|
||||
pub multiple_of: Option<f64>,
|
||||
pub minimum: Option<f64>,
|
||||
pub maximum: Option<f64>,
|
||||
#[serde(rename = "exclusiveMinimum")]
|
||||
pub exclusive_minimum: Option<f64>,
|
||||
#[serde(rename = "exclusiveMaximum")]
|
||||
pub exclusive_maximum: Option<f64>,
|
||||
|
||||
// Combining Keywords
|
||||
#[serde(rename = "allOf")]
|
||||
pub all_of: Option<Vec<Arc<Schema>>>,
|
||||
#[serde(rename = "anyOf")]
|
||||
pub any_of: Option<Vec<Arc<Schema>>>,
|
||||
#[serde(rename = "oneOf")]
|
||||
pub one_of: Option<Vec<Arc<Schema>>>,
|
||||
#[serde(rename = "not")]
|
||||
pub not: Option<Arc<Schema>>,
|
||||
#[serde(rename = "if")]
|
||||
pub if_: Option<Arc<Schema>>,
|
||||
#[serde(rename = "then")]
|
||||
pub then_: Option<Arc<Schema>>,
|
||||
#[serde(rename = "else")]
|
||||
pub else_: Option<Arc<Schema>>,
|
||||
|
||||
// Custom Vocabularies
|
||||
pub form: Option<Vec<String>>,
|
||||
pub display: Option<Vec<String>>,
|
||||
#[serde(rename = "enumNames")]
|
||||
pub enum_names: Option<Vec<String>>,
|
||||
pub control: Option<String>,
|
||||
pub actions: Option<BTreeMap<String, Action>>,
|
||||
pub computer: Option<String>,
|
||||
#[serde(default)]
|
||||
pub extensible: Option<bool>,
|
||||
|
||||
// Compiled Fields (Hidden from JSON/Serde)
|
||||
#[serde(skip)]
|
||||
pub compiled_format: Option<crate::compiler::CompiledFormat>,
|
||||
#[serde(skip)]
|
||||
pub compiled_pattern: Option<crate::compiler::CompiledRegex>,
|
||||
#[serde(skip)]
|
||||
pub compiled_pattern_properties: Option<Vec<(crate::compiler::CompiledRegex, Arc<Schema>)>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct Schema {
|
||||
#[serde(flatten)]
|
||||
pub obj: SchemaObject,
|
||||
#[serde(skip)]
|
||||
pub always_fail: bool,
|
||||
}
|
||||
|
||||
impl Default for Schema {
|
||||
fn default() -> Self {
|
||||
Schema {
|
||||
obj: SchemaObject::default(),
|
||||
always_fail: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for Schema {
|
||||
type Target = SchemaObject;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.obj
|
||||
}
|
||||
}
|
||||
impl std::ops::DerefMut for Schema {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.obj
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Schema {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let v: Value = Deserialize::deserialize(deserializer)?;
|
||||
|
||||
if let Some(b) = v.as_bool() {
|
||||
let mut obj = SchemaObject::default();
|
||||
if b {
|
||||
obj.extensible = Some(true);
|
||||
}
|
||||
return Ok(Schema {
|
||||
obj,
|
||||
always_fail: !b,
|
||||
});
|
||||
}
|
||||
let obj: SchemaObject = serde_json::from_value(v).map_err(serde::de::Error::custom)?;
|
||||
|
||||
Ok(Schema {
|
||||
obj,
|
||||
always_fail: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum SchemaTypeOrArray {
|
||||
Single(String),
|
||||
Multiple(Vec<String>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Action {
|
||||
pub navigate: Option<String>,
|
||||
pub punc: Option<String>,
|
||||
}
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum Dependency {
|
||||
Props(Vec<String>),
|
||||
Schema(Arc<Schema>),
|
||||
}
|
||||
2973
src/tests.rs
2973
src/tests.rs
File diff suppressed because it is too large
Load Diff
406
src/util.rs
Normal file
406
src/util.rs
Normal file
@ -0,0 +1,406 @@
|
||||
use serde::Deserialize;
|
||||
use std::fs;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct TestSuite {
|
||||
#[allow(dead_code)]
|
||||
description: String,
|
||||
schema: Option<serde_json::Value>,
|
||||
// Support JSPG-style test suites with explicit types/enums/puncs
|
||||
types: Option<serde_json::Value>,
|
||||
enums: Option<serde_json::Value>,
|
||||
puncs: Option<serde_json::Value>,
|
||||
tests: Vec<TestCase>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct TestCase {
|
||||
description: String,
|
||||
data: serde_json::Value,
|
||||
valid: bool,
|
||||
// Support explicit schema ID target for test case
|
||||
schema_id: Option<String>,
|
||||
// Expected output for masking tests
|
||||
#[allow(dead_code)]
|
||||
expected: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
use crate::registry::REGISTRY;
|
||||
use crate::validator::Validator;
|
||||
use serde_json::Value;
|
||||
|
||||
pub fn deserialize_some<'de, D>(deserializer: D) -> Result<Option<Value>, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let v = Value::deserialize(deserializer)?;
|
||||
Ok(Some(v))
|
||||
}
|
||||
|
||||
pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
|
||||
// Clear registry to ensure isolation
|
||||
{
|
||||
let mut registry = REGISTRY.write().unwrap();
|
||||
registry.clear();
|
||||
}
|
||||
|
||||
let content =
|
||||
fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read file: {}", path));
|
||||
let suite: Vec<TestSuite> = serde_json::from_str(&content)
|
||||
.unwrap_or_else(|e| panic!("Failed to parse JSON in {}: {}", path, e));
|
||||
|
||||
if index >= suite.len() {
|
||||
panic!("Index {} out of bounds for file {}", index, path);
|
||||
}
|
||||
|
||||
let group = &suite[index];
|
||||
let mut failures = Vec::<String>::new();
|
||||
|
||||
// Helper to register items with 'schemas'
|
||||
let register_schemas = |items_val: Option<&Value>| {
|
||||
if let Some(val) = items_val {
|
||||
if let Value::Array(arr) = val {
|
||||
for item in arr {
|
||||
if let Some(schemas_val) = item.get("schemas") {
|
||||
if let Value::Array(schemas) = schemas_val {
|
||||
for schema_val in schemas {
|
||||
if let Ok(schema) =
|
||||
serde_json::from_value::<crate::schema::Schema>(schema_val.clone())
|
||||
{
|
||||
// Clone ID upfront to avoid borrow issues
|
||||
if let Some(id_clone) = schema.obj.id.clone() {
|
||||
let mut registry = REGISTRY.write().unwrap();
|
||||
// Utilize the new compile method which handles strictness
|
||||
let compiled =
|
||||
crate::compiler::Compiler::compile(schema, Some(id_clone.clone()));
|
||||
registry.insert(id_clone, compiled);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 1. Register Family Schemas if 'types' is present
|
||||
if let Some(types_val) = &group.types {
|
||||
if let Value::Array(arr) = types_val {
|
||||
let mut family_map: std::collections::HashMap<String, std::collections::HashSet<String>> =
|
||||
std::collections::HashMap::new();
|
||||
|
||||
for item in arr {
|
||||
if let Some(name) = item.get("name").and_then(|v| v.as_str()) {
|
||||
if let Some(hierarchy) = item.get("hierarchy").and_then(|v| v.as_array()) {
|
||||
for ancestor in hierarchy {
|
||||
if let Some(anc_str) = ancestor.as_str() {
|
||||
family_map
|
||||
.entry(anc_str.to_string())
|
||||
.or_default()
|
||||
.insert(name.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (family_name, members) in family_map {
|
||||
let id = format!("{}.family", family_name);
|
||||
let object_refs: Vec<Value> = members
|
||||
.iter()
|
||||
.map(|s| serde_json::json!({ "$ref": s }))
|
||||
.collect();
|
||||
|
||||
let schema_json = serde_json::json!({
|
||||
"$id": id,
|
||||
"oneOf": object_refs
|
||||
});
|
||||
|
||||
if let Ok(schema) = serde_json::from_value::<crate::schema::Schema>(schema_json) {
|
||||
let mut registry = REGISTRY.write().unwrap();
|
||||
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone()));
|
||||
registry.insert(id, compiled);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Register items directly
|
||||
register_schemas(group.enums.as_ref());
|
||||
register_schemas(group.types.as_ref());
|
||||
register_schemas(group.puncs.as_ref());
|
||||
|
||||
// 3. Register root 'schemas' if present (generic test support)
|
||||
// Some tests use a raw 'schema' or 'schemas' field at the group level
|
||||
if let Some(schema_val) = &group.schema {
|
||||
if let Ok(schema) = serde_json::from_value::<crate::schema::Schema>(schema_val.clone()) {
|
||||
let id = schema
|
||||
.obj
|
||||
.id
|
||||
.clone()
|
||||
.or_else(|| {
|
||||
// Fallback ID if none provided in schema
|
||||
Some("root".to_string())
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let mut registry = REGISTRY.write().unwrap();
|
||||
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone()));
|
||||
registry.insert(id, compiled);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Run Tests
|
||||
for (test_index, test) in group.tests.iter().enumerate() {
|
||||
let mut schema_id = test.schema_id.clone();
|
||||
|
||||
// If no explicit schema_id, try to infer from the single schema in the group
|
||||
if schema_id.is_none() {
|
||||
if let Some(s) = &group.schema {
|
||||
// If 'schema' is a single object, use its ID or "root"
|
||||
if let Some(obj) = s.as_object() {
|
||||
if let Some(id_val) = obj.get("$id") {
|
||||
schema_id = id_val.as_str().map(|s| s.to_string());
|
||||
}
|
||||
}
|
||||
if schema_id.is_none() {
|
||||
schema_id = Some("root".to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Default to the first punc if present (for puncs.json style)
|
||||
if schema_id.is_none() {
|
||||
if let Some(Value::Array(puncs)) = &group.puncs {
|
||||
if let Some(first_punc) = puncs.first() {
|
||||
if let Some(Value::Array(schemas)) = first_punc.get("schemas") {
|
||||
if let Some(first_schema) = schemas.first() {
|
||||
if let Some(id) = first_schema.get("$id").and_then(|v| v.as_str()) {
|
||||
schema_id = Some(id.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(sid) = schema_id {
|
||||
let result = Validator::validate(&sid, &test.data);
|
||||
|
||||
if !result.errors.is_empty() != !test.valid {
|
||||
failures.push(format!(
|
||||
"[{}] Test '{}' failed. Expected: {}, Got: {}. Errors: {:?}",
|
||||
group.description,
|
||||
test.description,
|
||||
test.valid,
|
||||
!result.errors.is_empty(),
|
||||
result.errors
|
||||
));
|
||||
}
|
||||
} else {
|
||||
failures.push(format!(
|
||||
"[{}] Test '{}' skipped: No schema ID found.",
|
||||
group.description, test.description
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
if !failures.is_empty() {
|
||||
return Err(failures.join("\n"));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
pub fn run_test_file(path: &str) -> Result<(), String> {
|
||||
let content =
|
||||
fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read file: {}", path));
|
||||
let suite: Vec<TestSuite> = serde_json::from_str(&content)
|
||||
.unwrap_or_else(|e| panic!("Failed to parse JSON in {}: {}", path, e));
|
||||
|
||||
let mut failures = Vec::<String>::new();
|
||||
for (group_index, group) in suite.into_iter().enumerate() {
|
||||
// Helper to register items with 'schemas'
|
||||
let register_schemas = |items_val: Option<Value>| {
|
||||
if let Some(val) = items_val {
|
||||
if let Value::Array(arr) = val {
|
||||
for item in arr {
|
||||
if let Some(schemas_val) = item.get("schemas") {
|
||||
if let Value::Array(schemas) = schemas_val {
|
||||
for schema_val in schemas {
|
||||
if let Ok(schema) =
|
||||
serde_json::from_value::<crate::schema::Schema>(schema_val.clone())
|
||||
{
|
||||
// Clone ID upfront to avoid borrow issues
|
||||
if let Some(id_clone) = schema.obj.id.clone() {
|
||||
let mut registry = REGISTRY.write().unwrap();
|
||||
// Utilize the new compile method which handles strictness
|
||||
let compiled =
|
||||
crate::compiler::Compiler::compile(schema, Some(id_clone.clone()));
|
||||
registry.insert(id_clone, compiled);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 1. Register Family Schemas if 'types' is present
|
||||
if let Some(types_val) = &group.types {
|
||||
if let Value::Array(arr) = types_val {
|
||||
let mut family_map: std::collections::HashMap<String, std::collections::HashSet<String>> =
|
||||
std::collections::HashMap::new();
|
||||
|
||||
for item in arr {
|
||||
if let Some(name) = item.get("name").and_then(|v| v.as_str()) {
|
||||
// Default hierarchy contains self if not specified?
|
||||
// Usually hierarchy is explicit in these tests.
|
||||
if let Some(hierarchy) = item.get("hierarchy").and_then(|v| v.as_array()) {
|
||||
for ancestor in hierarchy {
|
||||
if let Some(anc_str) = ancestor.as_str() {
|
||||
family_map
|
||||
.entry(anc_str.to_string())
|
||||
.or_default()
|
||||
.insert(name.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (family_name, members) in family_map {
|
||||
let id = format!("{}.family", family_name);
|
||||
let object_refs: Vec<Value> = members
|
||||
.into_iter()
|
||||
.map(|s| serde_json::json!({ "$ref": s }))
|
||||
.collect();
|
||||
|
||||
let schema_json = serde_json::json!({
|
||||
"$id": id,
|
||||
"oneOf": object_refs
|
||||
});
|
||||
|
||||
if let Ok(schema) = serde_json::from_value::<crate::schema::Schema>(schema_json) {
|
||||
let mut registry = REGISTRY.write().unwrap();
|
||||
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone()));
|
||||
registry.insert(id, compiled);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Register 'types', 'enums', and 'puncs' if present (JSPG style)
|
||||
register_schemas(group.types);
|
||||
register_schemas(group.enums);
|
||||
register_schemas(group.puncs);
|
||||
|
||||
// Register main 'schema' if present (Standard style)
|
||||
// Ensure ID is a valid URI to avoid Url::parse errors in Compiler
|
||||
let unique_id = format!("test:{}:{}", path, group_index);
|
||||
|
||||
// Register main 'schema' if present (Standard style)
|
||||
if let Some(ref schema_val) = group.schema {
|
||||
let mut registry = REGISTRY.write().unwrap();
|
||||
let schema: crate::schema::Schema =
|
||||
serde_json::from_value(schema_val.clone()).expect("Failed to parse test schema");
|
||||
let compiled = crate::compiler::Compiler::compile(schema, Some(unique_id.clone()));
|
||||
registry.insert(unique_id.clone(), compiled);
|
||||
}
|
||||
|
||||
for test in group.tests {
|
||||
// Use explicit schema_id from test, or default to unique_id
|
||||
let schema_id = test.schema_id.as_deref().unwrap_or(&unique_id).to_string();
|
||||
|
||||
let drop = Validator::validate(&schema_id, &test.data);
|
||||
|
||||
if test.valid {
|
||||
if !drop.errors.is_empty() {
|
||||
let msg = format!(
|
||||
"Test failed (expected valid): {}\nSchema: {:?}\nData: {:?}\nErrors: {:?}",
|
||||
test.description,
|
||||
group.schema, // We might need to find the actual schema used if schema_id is custom
|
||||
test.data,
|
||||
drop.errors
|
||||
);
|
||||
eprintln!("{}", msg);
|
||||
failures.push(msg);
|
||||
}
|
||||
} else {
|
||||
if drop.errors.is_empty() {
|
||||
let msg = format!(
|
||||
"Test failed (expected invalid): {}\nSchema: {:?}\nData: {:?}\nErrors: (Empty)",
|
||||
test.description, group.schema, test.data
|
||||
);
|
||||
println!("{}", msg);
|
||||
failures.push(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !failures.is_empty() {
|
||||
return Err(format!(
|
||||
"{} tests failed in file {}:\n\n{}",
|
||||
failures.len(),
|
||||
path,
|
||||
failures.join("\n\n")
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_integer(v: &Value) -> bool {
|
||||
match v {
|
||||
Value::Number(n) => {
|
||||
n.is_i64() || n.is_u64() || n.as_f64().filter(|n| n.fract() == 0.0).is_some()
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// serde_json treats 0 and 0.0 not equal. so we cannot simply use v1==v2
|
||||
pub fn equals(v1: &Value, v2: &Value) -> bool {
|
||||
// eprintln!("Comparing {:?} with {:?}", v1, v2);
|
||||
match (v1, v2) {
|
||||
(Value::Null, Value::Null) => true,
|
||||
(Value::Bool(b1), Value::Bool(b2)) => b1 == b2,
|
||||
(Value::Number(n1), Value::Number(n2)) => {
|
||||
if let (Some(n1), Some(n2)) = (n1.as_u64(), n2.as_u64()) {
|
||||
return n1 == n2;
|
||||
}
|
||||
if let (Some(n1), Some(n2)) = (n1.as_i64(), n2.as_i64()) {
|
||||
return n1 == n2;
|
||||
}
|
||||
if let (Some(n1), Some(n2)) = (n1.as_f64(), n2.as_f64()) {
|
||||
return (n1 - n2).abs() < f64::EPSILON;
|
||||
}
|
||||
false
|
||||
}
|
||||
(Value::String(s1), Value::String(s2)) => s1 == s2,
|
||||
(Value::Array(arr1), Value::Array(arr2)) => {
|
||||
if arr1.len() != arr2.len() {
|
||||
return false;
|
||||
}
|
||||
arr1.iter().zip(arr2).all(|(e1, e2)| equals(e1, e2))
|
||||
}
|
||||
(Value::Object(obj1), Value::Object(obj2)) => {
|
||||
if obj1.len() != obj2.len() {
|
||||
return false;
|
||||
}
|
||||
for (k1, v1) in obj1 {
|
||||
if let Some(v2) = obj2.get(k1) {
|
||||
if !equals(v1, v2) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
1259
src/validator.rs
Normal file
1259
src/validator.rs
Normal file
File diff suppressed because it is too large
Load Diff
1
tests/fixtures/JSON-Schema-Test-Suite
vendored
Submodule
1
tests/fixtures/JSON-Schema-Test-Suite
vendored
Submodule
Submodule tests/fixtures/JSON-Schema-Test-Suite added at bce6a47cc3
563
tests/fixtures/allOf.json
vendored
Normal file
563
tests/fixtures/allOf.json
vendored
Normal file
@ -0,0 +1,563 @@
|
||||
[
|
||||
{
|
||||
"description": "allOf",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"properties": {
|
||||
"bar": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"bar"
|
||||
]
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"foo"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "allOf",
|
||||
"data": {
|
||||
"foo": "baz",
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "mismatch second",
|
||||
"data": {
|
||||
"foo": "baz"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "mismatch first",
|
||||
"data": {
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "wrong type",
|
||||
"data": {
|
||||
"foo": "baz",
|
||||
"bar": "quux"
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with base schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"bar": {
|
||||
"type": "integer"
|
||||
},
|
||||
"baz": {},
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"bar"
|
||||
],
|
||||
"allOf": [
|
||||
{
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"foo"
|
||||
]
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"baz": {
|
||||
"type": "null"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"baz"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid",
|
||||
"data": {
|
||||
"foo": "quux",
|
||||
"bar": 2,
|
||||
"baz": null
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "mismatch base schema",
|
||||
"data": {
|
||||
"foo": "quux",
|
||||
"baz": null
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "mismatch first allOf",
|
||||
"data": {
|
||||
"bar": 2,
|
||||
"baz": null
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "mismatch second allOf",
|
||||
"data": {
|
||||
"foo": "quux",
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "mismatch both",
|
||||
"data": {
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf simple types",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"maximum": 30
|
||||
},
|
||||
{
|
||||
"minimum": 20
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid",
|
||||
"data": 25,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "mismatch one",
|
||||
"data": 35,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with boolean schemas, all true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
true,
|
||||
true
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with boolean schemas, some false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
true,
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with boolean schemas, all false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with one empty schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any data is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with two empty schemas",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{},
|
||||
{}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any data is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with the first empty schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{},
|
||||
{
|
||||
"type": "number"
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "number is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "string is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with the last empty schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"type": "number"
|
||||
},
|
||||
{}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "number is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "string is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "nested allOf, to check validation semantics",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"allOf": [
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "null is valid",
|
||||
"data": null,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "anything non-null is invalid",
|
||||
"data": 123,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf combined with anyOf, oneOf",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"multipleOf": 2
|
||||
}
|
||||
],
|
||||
"anyOf": [
|
||||
{
|
||||
"multipleOf": 3
|
||||
}
|
||||
],
|
||||
"oneOf": [
|
||||
{
|
||||
"multipleOf": 5
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "allOf: false, anyOf: false, oneOf: false",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "allOf: false, anyOf: false, oneOf: true",
|
||||
"data": 5,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "allOf: false, anyOf: true, oneOf: false",
|
||||
"data": 3,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "allOf: false, anyOf: true, oneOf: true",
|
||||
"data": 15,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "allOf: true, anyOf: false, oneOf: false",
|
||||
"data": 2,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "allOf: true, anyOf: false, oneOf: true",
|
||||
"data": 10,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "allOf: true, anyOf: true, oneOf: false",
|
||||
"data": 6,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "allOf: true, anyOf: true, oneOf: true",
|
||||
"data": 30,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows extra properties in allOf",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"properties": {
|
||||
"bar": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"bar"
|
||||
]
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"foo"
|
||||
]
|
||||
}
|
||||
],
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra property is valid",
|
||||
"data": {
|
||||
"foo": "baz",
|
||||
"bar": 2,
|
||||
"qux": 3
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "strict by default with allOf properties",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"properties": {
|
||||
"foo": {
|
||||
"const": 1
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"bar": {
|
||||
"const": 2
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "validates merged properties",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "fails on extra property z explicitly",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2,
|
||||
"z": 3
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with nested extensible: true (partial looseness)",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"properties": {
|
||||
"foo": {
|
||||
"const": 1
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"extensible": true,
|
||||
"properties": {
|
||||
"bar": {
|
||||
"const": 2
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extensible subschema doesn't make root extensible if root is strict",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2,
|
||||
"z": 3
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "strictness: allOf composition with strict refs",
|
||||
"schema": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/$defs/partA"
|
||||
},
|
||||
{
|
||||
"$ref": "#/$defs/partB"
|
||||
}
|
||||
],
|
||||
"$defs": {
|
||||
"partA": {
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"partB": {
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "merged instance is valid",
|
||||
"data": {
|
||||
"id": "1",
|
||||
"name": "Me"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "extra property is invalid (root is strict)",
|
||||
"data": {
|
||||
"id": "1",
|
||||
"name": "Me",
|
||||
"extra": 1
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "partA mismatch is invalid",
|
||||
"data": {
|
||||
"id": 1,
|
||||
"name": "Me"
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
120
tests/fixtures/anchor.json
vendored
Normal file
120
tests/fixtures/anchor.json
vendored
Normal file
@ -0,0 +1,120 @@
|
||||
[
|
||||
{
|
||||
"description": "Location-independent identifier",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$ref": "#foo",
|
||||
"$defs": {
|
||||
"A": {
|
||||
"$anchor": "foo",
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"data": 1,
|
||||
"description": "match",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"data": "a",
|
||||
"description": "mismatch",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "Location-independent identifier with absolute URI",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$ref": "http://localhost:1234/draft2020-12/bar#foo",
|
||||
"$defs": {
|
||||
"A": {
|
||||
"$id": "http://localhost:1234/draft2020-12/bar",
|
||||
"$anchor": "foo",
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"data": 1,
|
||||
"description": "match",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"data": "a",
|
||||
"description": "mismatch",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "Location-independent identifier with base URI change in subschema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "http://localhost:1234/draft2020-12/root",
|
||||
"$ref": "http://localhost:1234/draft2020-12/nested.json#foo",
|
||||
"$defs": {
|
||||
"A": {
|
||||
"$id": "nested.json",
|
||||
"$defs": {
|
||||
"B": {
|
||||
"$anchor": "foo",
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"data": 1,
|
||||
"description": "match",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"data": "a",
|
||||
"description": "mismatch",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "same $anchor with different base uri",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "http://localhost:1234/draft2020-12/foobar",
|
||||
"$defs": {
|
||||
"A": {
|
||||
"$id": "child1",
|
||||
"allOf": [
|
||||
{
|
||||
"$id": "child2",
|
||||
"$anchor": "my_anchor",
|
||||
"type": "number"
|
||||
},
|
||||
{
|
||||
"$anchor": "my_anchor",
|
||||
"type": "string"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"$ref": "child1#my_anchor"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "$ref resolves to /$defs/A/allOf/1",
|
||||
"data": "a",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "$ref does not resolve to /$defs/A/allOf/0",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
295
tests/fixtures/anyOf.json
vendored
Normal file
295
tests/fixtures/anyOf.json
vendored
Normal file
@ -0,0 +1,295 @@
|
||||
[
|
||||
{
|
||||
"description": "anyOf",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "integer"
|
||||
},
|
||||
{
|
||||
"minimum": 2
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "first anyOf valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "second anyOf valid",
|
||||
"data": 2.5,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "both anyOf valid",
|
||||
"data": 3,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "neither anyOf valid",
|
||||
"data": 1.5,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "anyOf with base schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "string",
|
||||
"anyOf": [
|
||||
{
|
||||
"maxLength": 2
|
||||
},
|
||||
{
|
||||
"minLength": 4
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "mismatch base schema",
|
||||
"data": 3,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "one anyOf valid",
|
||||
"data": "foobar",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "both anyOf invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "anyOf with boolean schemas, all true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
true,
|
||||
true
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "anyOf with boolean schemas, some true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
true,
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "anyOf with boolean schemas, all false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "anyOf complex types",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
{
|
||||
"properties": {
|
||||
"bar": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"bar"
|
||||
]
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"foo"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "first anyOf valid (complex)",
|
||||
"data": {
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "second anyOf valid (complex)",
|
||||
"data": {
|
||||
"foo": "baz"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "both anyOf valid (complex)",
|
||||
"data": {
|
||||
"foo": "baz",
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "neither anyOf valid (complex)",
|
||||
"data": {
|
||||
"foo": 2,
|
||||
"bar": "quux"
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "anyOf with one empty schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "number"
|
||||
},
|
||||
{}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "string is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "number is valid",
|
||||
"data": 123,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "nested anyOf, to check validation semantics",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
{
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "null is valid",
|
||||
"data": null,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "anything non-null is invalid",
|
||||
"data": 123,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows extra properties in anyOf",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "integer"
|
||||
},
|
||||
{
|
||||
"minimum": 2
|
||||
}
|
||||
],
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra property is valid",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "strict by default with anyOf properties",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
{
|
||||
"properties": {
|
||||
"foo": {
|
||||
"const": 1
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"bar": {
|
||||
"const": 2
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid match (foo)",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "fails on extra property z explicitly",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"z": 3
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
112
tests/fixtures/boolean_schema.json
vendored
Normal file
112
tests/fixtures/boolean_schema.json
vendored
Normal file
@ -0,0 +1,112 @@
|
||||
[
|
||||
{
|
||||
"description": "boolean schema 'true'",
|
||||
"schema": true,
|
||||
"tests": [
|
||||
{
|
||||
"description": "number is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "string is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boolean true is valid",
|
||||
"data": true,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boolean false is valid",
|
||||
"data": false,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "null is valid",
|
||||
"data": null,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "object is valid",
|
||||
"data": {
|
||||
"foo": "bar"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty object is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array is valid",
|
||||
"data": [
|
||||
"foo"
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty array is valid",
|
||||
"data": [],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "boolean schema 'false'",
|
||||
"schema": false,
|
||||
"tests": [
|
||||
{
|
||||
"description": "number is invalid",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "string is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "boolean true is invalid",
|
||||
"data": true,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "boolean false is invalid",
|
||||
"data": false,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "null is invalid",
|
||||
"data": null,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "object is invalid",
|
||||
"data": {
|
||||
"foo": "bar"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty object is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "array is invalid",
|
||||
"data": [
|
||||
"foo"
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
522
tests/fixtures/const.json
vendored
Normal file
522
tests/fixtures/const.json
vendored
Normal file
@ -0,0 +1,522 @@
|
||||
[
|
||||
{
|
||||
"description": "const validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": 2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "same value is valid",
|
||||
"data": 2,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "another value is invalid",
|
||||
"data": 5,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "another type is invalid",
|
||||
"data": "a",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with object",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": {
|
||||
"foo": "bar",
|
||||
"baz": "bax"
|
||||
},
|
||||
"properties": {
|
||||
"foo": {},
|
||||
"baz": {}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "same object is valid",
|
||||
"data": {
|
||||
"foo": "bar",
|
||||
"baz": "bax"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "same object with different property order is valid",
|
||||
"data": {
|
||||
"baz": "bax",
|
||||
"foo": "bar"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "another object is invalid",
|
||||
"data": {
|
||||
"foo": "bar"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "another type is invalid",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with array",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": [
|
||||
{
|
||||
"foo": "bar"
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "same array is valid",
|
||||
"data": [
|
||||
{
|
||||
"foo": "bar"
|
||||
}
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "another array item is invalid",
|
||||
"data": [
|
||||
2
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "array with additional items is invalid",
|
||||
"data": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with null",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": null
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "null is valid",
|
||||
"data": null,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "not null is invalid",
|
||||
"data": 0,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with false does not match 0",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "false is valid",
|
||||
"data": false,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "integer zero is invalid",
|
||||
"data": 0,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "float zero is invalid",
|
||||
"data": 0.0,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with true does not match 1",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "true is valid",
|
||||
"data": true,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "integer one is invalid",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "float one is invalid",
|
||||
"data": 1.0,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with [false] does not match [0]",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "[false] is valid",
|
||||
"data": [
|
||||
false
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "[0] is invalid",
|
||||
"data": [
|
||||
0
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "[0.0] is invalid",
|
||||
"data": [
|
||||
0.0
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with [true] does not match [1]",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": [
|
||||
true
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "[true] is valid",
|
||||
"data": [
|
||||
true
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "[1] is invalid",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "[1.0] is invalid",
|
||||
"data": [
|
||||
1.0
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with {\"a\": false} does not match {\"a\": 0}",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": {
|
||||
"a": false
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "{\"a\": false} is valid",
|
||||
"data": {
|
||||
"a": false
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "{\"a\": 0} is invalid",
|
||||
"data": {
|
||||
"a": 0
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "{\"a\": 0.0} is invalid",
|
||||
"data": {
|
||||
"a": 0.0
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with {\"a\": true} does not match {\"a\": 1}",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": {
|
||||
"a": true
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "{\"a\": true} is valid",
|
||||
"data": {
|
||||
"a": true
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "{\"a\": 1} is invalid",
|
||||
"data": {
|
||||
"a": 1
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "{\"a\": 1.0} is invalid",
|
||||
"data": {
|
||||
"a": 1.0
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with 0 does not match other zero-like types",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": 0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "false is invalid",
|
||||
"data": false,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "integer zero is valid",
|
||||
"data": 0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "float zero is valid",
|
||||
"data": 0.0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty object is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty string is invalid",
|
||||
"data": "",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with 1 does not match true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": 1
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "true is invalid",
|
||||
"data": true,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "integer one is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "float one is valid",
|
||||
"data": 1.0,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with -2.0 matches integer and float types",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": -2.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "integer -2 is valid",
|
||||
"data": -2,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "integer 2 is invalid",
|
||||
"data": 2,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "float -2.0 is valid",
|
||||
"data": -2.0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "float 2.0 is invalid",
|
||||
"data": 2.0,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "float -2.00001 is invalid",
|
||||
"data": -2.00001,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "float and integers are equal up to 64-bit representation limits",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": 9007199254740992
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "integer is valid",
|
||||
"data": 9007199254740992,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "integer minus one is invalid",
|
||||
"data": 9007199254740991,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "float is valid",
|
||||
"data": 9007199254740992.0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "float minus one is invalid",
|
||||
"data": 9007199254740991.0,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "nul characters in strings",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": "hello\u0000there"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "match string with nul",
|
||||
"data": "hello\u0000there",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "do not match string lacking nul",
|
||||
"data": "hellothere",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "characters with the same visual representation but different codepoint",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": "μ",
|
||||
"$comment": "U+03BC"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "character uses the same codepoint",
|
||||
"data": "μ",
|
||||
"comment": "U+03BC",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "character looks the same but uses a different codepoint",
|
||||
"data": "µ",
|
||||
"comment": "U+00B5",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "characters with the same visual representation, but different number of codepoints",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": "ä",
|
||||
"$comment": "U+00E4"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "character uses the same codepoint",
|
||||
"data": "ä",
|
||||
"comment": "U+00E4",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "character looks the same but uses combining marks",
|
||||
"data": "ä",
|
||||
"comment": "a, U+0308",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows extra properties in const object match",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": {
|
||||
"a": 1
|
||||
},
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra property ignored during strict check, but const check still applies (mismatch)",
|
||||
"data": {
|
||||
"a": 1,
|
||||
"b": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "extra property match in const (this is effectively impossible if data has extra props not in const, it implicitly fails const check unless we assume const check ignored extra props? No, const check is strict. So this test is just to show strictness passes.)",
|
||||
"data": {
|
||||
"a": 1
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
286
tests/fixtures/contains.json
vendored
Normal file
286
tests/fixtures/contains.json
vendored
Normal file
@ -0,0 +1,286 @@
|
||||
[
|
||||
{
|
||||
"description": "contains keyword validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"minimum": 5
|
||||
},
|
||||
"items": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "array with item matching schema (5) is valid (items: true)",
|
||||
"data": [
|
||||
3,
|
||||
4,
|
||||
5
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array with item matching schema (6) is valid (items: true)",
|
||||
"data": [
|
||||
3,
|
||||
4,
|
||||
6
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array with two items matching schema (5, 6) is valid (items: true)",
|
||||
"data": [
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array without items matching schema is invalid",
|
||||
"data": [
|
||||
2,
|
||||
3,
|
||||
4
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "not array is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "contains keyword with const keyword",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 5
|
||||
},
|
||||
"items": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "array with item 5 is valid (items: true)",
|
||||
"data": [
|
||||
3,
|
||||
4,
|
||||
5
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array with two items 5 is valid (items: true)",
|
||||
"data": [
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
5
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array without item 5 is invalid",
|
||||
"data": [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "contains keyword with boolean schema true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any non-empty array is valid",
|
||||
"data": [
|
||||
"foo"
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "contains keyword with boolean schema false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any non-empty array is invalid",
|
||||
"data": [
|
||||
"foo"
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "non-arrays are valid",
|
||||
"data": "contains does not apply to strings",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "items + contains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"items": {
|
||||
"multipleOf": 2
|
||||
},
|
||||
"contains": {
|
||||
"multipleOf": 3
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "matches items, does not match contains",
|
||||
"data": [
|
||||
2,
|
||||
4,
|
||||
8
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "does not match items, matches contains",
|
||||
"data": [
|
||||
3,
|
||||
6,
|
||||
9
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "matches both items and contains",
|
||||
"data": [
|
||||
6,
|
||||
12
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "matches neither items nor contains",
|
||||
"data": [
|
||||
1,
|
||||
5
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "contains with false if subschema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"if": false,
|
||||
"else": true
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any non-empty array is valid",
|
||||
"data": [
|
||||
"foo"
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "contains with null instance elements",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"type": "null"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "allows null items",
|
||||
"data": [
|
||||
null
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows non-matching items in contains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 1
|
||||
},
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra items acceptable",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "strict by default: non-matching items in contains are invalid",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 1
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra items cause failure",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "only matching items is valid",
|
||||
"data": [
|
||||
1,
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
144
tests/fixtures/content.json
vendored
Normal file
144
tests/fixtures/content.json
vendored
Normal file
@ -0,0 +1,144 @@
|
||||
[
|
||||
{
|
||||
"description": "validation of string-encoded content based on media type",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contentMediaType": "application/json"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "a valid JSON document",
|
||||
"data": "{\"foo\": \"bar\"}",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an invalid JSON document; validates true",
|
||||
"data": "{:}",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "ignores non-strings",
|
||||
"data": 100,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "validation of binary string-encoding",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contentEncoding": "base64"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "a valid base64 string",
|
||||
"data": "eyJmb28iOiAiYmFyIn0K",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an invalid base64 string (% is not a valid character); validates true",
|
||||
"data": "eyJmb28iOi%iYmFyIn0K",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "ignores non-strings",
|
||||
"data": 100,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "validation of binary-encoded media type documents",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contentMediaType": "application/json",
|
||||
"contentEncoding": "base64"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "a valid base64-encoded JSON document",
|
||||
"data": "eyJmb28iOiAiYmFyIn0K",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "a validly-encoded invalid JSON document; validates true",
|
||||
"data": "ezp9Cg==",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an invalid base64 string that is valid JSON; validates true",
|
||||
"data": "{}",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "ignores non-strings",
|
||||
"data": 100,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "validation of binary-encoded media type documents with schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contentMediaType": "application/json",
|
||||
"contentEncoding": "base64",
|
||||
"contentSchema": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"foo"
|
||||
],
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string"
|
||||
},
|
||||
"boo": {
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "a valid base64-encoded JSON document",
|
||||
"data": "eyJmb28iOiAiYmFyIn0K",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "another valid base64-encoded JSON document",
|
||||
"data": "eyJib28iOiAyMCwgImZvbyI6ICJiYXoifQ==",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an invalid base64-encoded JSON document; validates true",
|
||||
"data": "eyJib28iOiAyMH0=",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an empty object as a base64-encoded JSON document; validates true",
|
||||
"data": "e30=",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an empty array as a base64-encoded JSON document",
|
||||
"data": "W10=",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "a validly-encoded invalid JSON document; validates true",
|
||||
"data": "ezp9Cg==",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an invalid base64 string that is valid JSON; validates true",
|
||||
"data": "{}",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "ignores non-strings",
|
||||
"data": 100,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
220
tests/fixtures/dependentRequired.json
vendored
Normal file
220
tests/fixtures/dependentRequired.json
vendored
Normal file
@ -0,0 +1,220 @@
|
||||
[
|
||||
{
|
||||
"description": "single dependency",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"dependentRequired": {
|
||||
"bar": [
|
||||
"foo"
|
||||
]
|
||||
},
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "neither",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "nondependant",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "with dependency",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "missing dependency",
|
||||
"data": {
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "ignores arrays",
|
||||
"data": [
|
||||
"bar"
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "ignores strings",
|
||||
"data": "foobar",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "ignores other non-objects",
|
||||
"data": 12,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "empty dependents",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"dependentRequired": {
|
||||
"bar": []
|
||||
},
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty object",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "object with one property",
|
||||
"data": {
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "non-object is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "multiple dependents required",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"dependentRequired": {
|
||||
"quux": [
|
||||
"foo",
|
||||
"bar"
|
||||
]
|
||||
},
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "neither",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "nondependants",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "with dependencies",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2,
|
||||
"quux": 3
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "missing dependency",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"quux": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "missing other dependency",
|
||||
"data": {
|
||||
"bar": 1,
|
||||
"quux": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "missing both dependencies",
|
||||
"data": {
|
||||
"quux": 1
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "dependencies with escaped characters",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"dependentRequired": {
|
||||
"foo\nbar": [
|
||||
"foo\rbar"
|
||||
],
|
||||
"foo\"bar": [
|
||||
"foo'bar"
|
||||
]
|
||||
},
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "CRLF",
|
||||
"data": {
|
||||
"foo\nbar": 1,
|
||||
"foo\rbar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "quoted quotes",
|
||||
"data": {
|
||||
"foo'bar": 1,
|
||||
"foo\"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "CRLF missing dependent",
|
||||
"data": {
|
||||
"foo\nbar": 1,
|
||||
"foo": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "quoted quotes missing dependent",
|
||||
"data": {
|
||||
"foo\"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows extra properties in dependentRequired",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"dependentRequired": {
|
||||
"bar": [
|
||||
"foo"
|
||||
]
|
||||
},
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra property is valid",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2,
|
||||
"baz": 3
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
233
tests/fixtures/dependentSchemas.json
vendored
Normal file
233
tests/fixtures/dependentSchemas.json
vendored
Normal file
@ -0,0 +1,233 @@
|
||||
[
|
||||
{
|
||||
"description": "single dependency",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"foo": true,
|
||||
"bar": true
|
||||
},
|
||||
"dependentSchemas": {
|
||||
"bar": {
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "integer"
|
||||
},
|
||||
"bar": {
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "no dependency",
|
||||
"data": {
|
||||
"foo": "quux"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "wrong type",
|
||||
"data": {
|
||||
"foo": "quux",
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "wrong type other",
|
||||
"data": {
|
||||
"foo": 2,
|
||||
"bar": "quux"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "wrong type both",
|
||||
"data": {
|
||||
"foo": "quux",
|
||||
"bar": "quux"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "ignores arrays",
|
||||
"data": [
|
||||
"bar"
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "ignores strings",
|
||||
"data": "foobar",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "ignores other non-objects",
|
||||
"data": 12,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "boolean subschemas",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"foo": true,
|
||||
"bar": true
|
||||
},
|
||||
"dependentSchemas": {
|
||||
"foo": true,
|
||||
"bar": false
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "object with property having schema true is valid",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "object with property having schema false is invalid",
|
||||
"data": {
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "object with both properties is invalid",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty object is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "dependencies with escaped characters",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"foo\tbar": true,
|
||||
"foo'bar": true,
|
||||
"a": true,
|
||||
"b": true,
|
||||
"c": true
|
||||
},
|
||||
"dependentSchemas": {
|
||||
"foo\tbar": {
|
||||
"minProperties": 4
|
||||
},
|
||||
"foo'bar": {
|
||||
"required": [
|
||||
"foo\"bar"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "quoted tab",
|
||||
"data": {
|
||||
"foo\tbar": 1,
|
||||
"a": 2,
|
||||
"b": 3,
|
||||
"c": 4
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "quoted quote",
|
||||
"data": {
|
||||
"foo'bar": {
|
||||
"foo\"bar": 1
|
||||
}
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "quoted tab invalid under dependent schema",
|
||||
"data": {
|
||||
"foo\tbar": 1,
|
||||
"a": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "quoted quote invalid under dependent schema",
|
||||
"data": {
|
||||
"foo'bar": 1
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "dependent subschema incompatible with root",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"foo": {},
|
||||
"baz": true
|
||||
},
|
||||
"dependentSchemas": {
|
||||
"foo": {
|
||||
"properties": {
|
||||
"bar": {}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "matches root",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "matches dependency",
|
||||
"data": {
|
||||
"bar": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "matches both",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "no dependency",
|
||||
"data": {
|
||||
"baz": 1
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
1010
tests/fixtures/dynamicRef.json
vendored
Normal file
1010
tests/fixtures/dynamicRef.json
vendored
Normal file
File diff suppressed because it is too large
Load Diff
119
tests/fixtures/emptyString.json
vendored
Normal file
119
tests/fixtures/emptyString.json
vendored
Normal file
@ -0,0 +1,119 @@
|
||||
[
|
||||
{
|
||||
"description": "empty string is valid for all types (except const)",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"obj": {
|
||||
"type": "object"
|
||||
},
|
||||
"arr": {
|
||||
"type": "array"
|
||||
},
|
||||
"str": {
|
||||
"type": "string"
|
||||
},
|
||||
"int": {
|
||||
"type": "integer"
|
||||
},
|
||||
"num": {
|
||||
"type": "number"
|
||||
},
|
||||
"bool": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"nul": {
|
||||
"type": "null"
|
||||
},
|
||||
"fmt": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
},
|
||||
"con": {
|
||||
"const": "value"
|
||||
},
|
||||
"con_empty": {
|
||||
"const": ""
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty string valid for object",
|
||||
"data": {
|
||||
"obj": ""
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty string valid for array",
|
||||
"data": {
|
||||
"arr": ""
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty string valid for string",
|
||||
"data": {
|
||||
"str": ""
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty string valid for integer",
|
||||
"data": {
|
||||
"int": ""
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty string valid for number",
|
||||
"data": {
|
||||
"num": ""
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty string valid for boolean",
|
||||
"data": {
|
||||
"bool": ""
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty string valid for null",
|
||||
"data": {
|
||||
"nul": ""
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty string valid for format",
|
||||
"data": {
|
||||
"fmt": ""
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty string INVALID for const (unless const is empty string)",
|
||||
"data": {
|
||||
"con": ""
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "CONST_VIOLATED",
|
||||
"path": "/con"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "empty string VALID for const if const IS empty string",
|
||||
"data": {
|
||||
"con_empty": ""
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
488
tests/fixtures/enum.json
vendored
Normal file
488
tests/fixtures/enum.json
vendored
Normal file
@ -0,0 +1,488 @@
|
||||
[
|
||||
{
|
||||
"description": "simple enum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "one of the enum is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "something else is invalid",
|
||||
"data": 4,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "heterogeneous enum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
6,
|
||||
"foo",
|
||||
[],
|
||||
true,
|
||||
{
|
||||
"foo": 12
|
||||
}
|
||||
],
|
||||
"properties": {
|
||||
"foo": {}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "one of the enum is valid",
|
||||
"data": [],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "something else is invalid",
|
||||
"data": null,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "objects are deep compared",
|
||||
"data": {
|
||||
"foo": false
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "valid object matches",
|
||||
"data": {
|
||||
"foo": 12
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "extra properties in object is invalid",
|
||||
"data": {
|
||||
"foo": 12,
|
||||
"boo": 42
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "heterogeneous enum-with-null validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
6,
|
||||
null
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "null is valid",
|
||||
"data": null,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "number is valid",
|
||||
"data": 6,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "something else is invalid",
|
||||
"data": "test",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enums in properties",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"foo": {
|
||||
"enum": [
|
||||
"foo"
|
||||
]
|
||||
},
|
||||
"bar": {
|
||||
"enum": [
|
||||
"bar"
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"bar"
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "both properties are valid",
|
||||
"data": {
|
||||
"foo": "foo",
|
||||
"bar": "bar"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "wrong foo value",
|
||||
"data": {
|
||||
"foo": "foot",
|
||||
"bar": "bar"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "wrong bar value",
|
||||
"data": {
|
||||
"foo": "foo",
|
||||
"bar": "bart"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "missing optional property is valid",
|
||||
"data": {
|
||||
"bar": "bar"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "missing required property is invalid",
|
||||
"data": {
|
||||
"foo": "foo"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "missing all properties is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with escaped characters",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
"foo\nbar",
|
||||
"foo\rbar"
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "member 1 is valid",
|
||||
"data": "foo\nbar",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "member 2 is valid",
|
||||
"data": "foo\rbar",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "another string is invalid",
|
||||
"data": "abc",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with false does not match 0",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "false is valid",
|
||||
"data": false,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "integer zero is invalid",
|
||||
"data": 0,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "float zero is invalid",
|
||||
"data": 0.0,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with [false] does not match [0]",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
[
|
||||
false
|
||||
]
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "[false] is valid",
|
||||
"data": [
|
||||
false
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "[0] is invalid",
|
||||
"data": [
|
||||
0
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "[0.0] is invalid",
|
||||
"data": [
|
||||
0.0
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with true does not match 1",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
true
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "true is valid",
|
||||
"data": true,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "integer one is invalid",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "float one is invalid",
|
||||
"data": 1.0,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with [true] does not match [1]",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
[
|
||||
true
|
||||
]
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "[true] is valid",
|
||||
"data": [
|
||||
true
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "[1] is invalid",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "[1.0] is invalid",
|
||||
"data": [
|
||||
1.0
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with 0 does not match false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
0
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "false is invalid",
|
||||
"data": false,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "integer zero is valid",
|
||||
"data": 0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "float zero is valid",
|
||||
"data": 0.0,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with [0] does not match [false]",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
[
|
||||
0
|
||||
]
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "[false] is invalid",
|
||||
"data": [
|
||||
false
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "[0] is valid",
|
||||
"data": [
|
||||
0
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "[0.0] is valid",
|
||||
"data": [
|
||||
0.0
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with 1 does not match true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
1
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "true is invalid",
|
||||
"data": true,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "integer one is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "float one is valid",
|
||||
"data": 1.0,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with [1] does not match [true]",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
[
|
||||
1
|
||||
]
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "[true] is invalid",
|
||||
"data": [
|
||||
true
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "[1] is valid",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "[1.0] is valid",
|
||||
"data": [
|
||||
1.0
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "nul characters in strings",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
"hello\u0000there"
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "match string with nul",
|
||||
"data": "hello\u0000there",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "do not match string lacking nul",
|
||||
"data": "hellothere",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows extra properties in enum object match",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
{
|
||||
"foo": 1
|
||||
}
|
||||
],
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra property ignored during strict check, but enum check still applies (mismatch here)",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "extra property ignored during strict check, enum match succeeds",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
31
tests/fixtures/exclusiveMaximum.json
vendored
Normal file
31
tests/fixtures/exclusiveMaximum.json
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
[
|
||||
{
|
||||
"description": "exclusiveMaximum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"exclusiveMaximum": 3.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "below the exclusiveMaximum is valid",
|
||||
"data": 2.2,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point is invalid",
|
||||
"data": 3.0,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "above the exclusiveMaximum is invalid",
|
||||
"data": 3.5,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "ignores non-numbers",
|
||||
"data": "x",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
31
tests/fixtures/exclusiveMinimum.json
vendored
Normal file
31
tests/fixtures/exclusiveMinimum.json
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
[
|
||||
{
|
||||
"description": "exclusiveMinimum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"exclusiveMinimum": 1.1
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "above the exclusiveMinimum is valid",
|
||||
"data": 1.2,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point is invalid",
|
||||
"data": 1.1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "below the exclusiveMinimum is invalid",
|
||||
"data": 0.6,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "ignores non-numbers",
|
||||
"data": "x",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
3112
tests/fixtures/format.json
vendored
Normal file
3112
tests/fixtures/format.json
vendored
Normal file
File diff suppressed because it is too large
Load Diff
404
tests/fixtures/if-then-else.json
vendored
Normal file
404
tests/fixtures/if-then-else.json
vendored
Normal file
@ -0,0 +1,404 @@
|
||||
[
|
||||
{
|
||||
"description": "ignore if without then or else",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"if": {
|
||||
"const": 0
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid when valid against lone if",
|
||||
"data": 0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "valid when invalid against lone if",
|
||||
"data": "hello",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "ignore then without if",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"then": {
|
||||
"const": 0
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid when valid against lone then",
|
||||
"data": 0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "valid when invalid against lone then",
|
||||
"data": "hello",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "ignore else without if",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"else": {
|
||||
"const": 0
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid when valid against lone else",
|
||||
"data": 0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "valid when invalid against lone else",
|
||||
"data": "hello",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "if and then without else",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"if": {
|
||||
"exclusiveMaximum": 0
|
||||
},
|
||||
"then": {
|
||||
"minimum": -10
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid through then",
|
||||
"data": -1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid through then",
|
||||
"data": -100,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "valid when if test fails",
|
||||
"data": 3,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "if and else without then",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"if": {
|
||||
"exclusiveMaximum": 0
|
||||
},
|
||||
"else": {
|
||||
"multipleOf": 2
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid when if test passes",
|
||||
"data": -1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "valid through else",
|
||||
"data": 4,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid through else",
|
||||
"data": 3,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "validate against correct branch, then vs else",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"if": {
|
||||
"exclusiveMaximum": 0
|
||||
},
|
||||
"then": {
|
||||
"minimum": -10
|
||||
},
|
||||
"else": {
|
||||
"multipleOf": 2
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid through then",
|
||||
"data": -1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid through then",
|
||||
"data": -100,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "valid through else",
|
||||
"data": 4,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid through else",
|
||||
"data": 3,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "non-interference across combined schemas",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"if": {
|
||||
"exclusiveMaximum": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"then": {
|
||||
"minimum": -10
|
||||
}
|
||||
},
|
||||
{
|
||||
"else": {
|
||||
"multipleOf": 2
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid, but would have been invalid through then",
|
||||
"data": -100,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "valid, but would have been invalid through else",
|
||||
"data": 3,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "if with boolean schema true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"if": true,
|
||||
"then": {
|
||||
"const": "then"
|
||||
},
|
||||
"else": {
|
||||
"const": "else"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "boolean schema true in if always chooses the then path (valid)",
|
||||
"data": "then",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boolean schema true in if always chooses the then path (invalid)",
|
||||
"data": "else",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "if with boolean schema false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"if": false,
|
||||
"then": {
|
||||
"const": "then"
|
||||
},
|
||||
"else": {
|
||||
"const": "else"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "boolean schema false in if always chooses the else path (invalid)",
|
||||
"data": "then",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "boolean schema false in if always chooses the else path (valid)",
|
||||
"data": "else",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "if appears at the end when serialized (keyword processing sequence)",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"then": {
|
||||
"const": "yes"
|
||||
},
|
||||
"else": {
|
||||
"const": "other"
|
||||
},
|
||||
"if": {
|
||||
"maxLength": 4
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "yes redirects to then and passes",
|
||||
"data": "yes",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "other redirects to else and passes",
|
||||
"data": "other",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "no redirects to then and fails",
|
||||
"data": "no",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "invalid redirects to else and fails",
|
||||
"data": "invalid",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "then: false fails when condition matches",
|
||||
"schema": {
|
||||
"if": {
|
||||
"const": 1
|
||||
},
|
||||
"then": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "matches if → then=false → invalid",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "does not match if → then ignored → valid",
|
||||
"data": 2,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "else: false fails when condition does not match",
|
||||
"schema": {
|
||||
"if": {
|
||||
"const": 1
|
||||
},
|
||||
"else": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "matches if → else ignored → valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "does not match if → else executes → invalid",
|
||||
"data": 2,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows extra properties in if-then-else",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"if": {
|
||||
"properties": {
|
||||
"foo": {
|
||||
"const": 1
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"foo"
|
||||
]
|
||||
},
|
||||
"then": {
|
||||
"properties": {
|
||||
"bar": {
|
||||
"const": 2
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"bar"
|
||||
]
|
||||
},
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra property is valid (matches if and then)",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2,
|
||||
"extra": "prop"
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "strict by default with if-then properties",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"if": {
|
||||
"properties": {
|
||||
"foo": {
|
||||
"const": 1
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"foo"
|
||||
]
|
||||
},
|
||||
"then": {
|
||||
"properties": {
|
||||
"bar": {
|
||||
"const": 2
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid match (foo + bar)",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "fails on extra property z explicitly",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2,
|
||||
"z": 3
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
738
tests/fixtures/items.json
vendored
Normal file
738
tests/fixtures/items.json
vendored
Normal file
@ -0,0 +1,738 @@
|
||||
[
|
||||
{
|
||||
"description": "a schema given for items",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"items": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid items",
|
||||
"data": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "wrong type of items",
|
||||
"data": [
|
||||
1,
|
||||
"x"
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "non-arrays are invalid",
|
||||
"data": {
|
||||
"foo": "bar"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "JavaScript pseudo-arrays are invalid",
|
||||
"data": {
|
||||
"0": "invalid",
|
||||
"length": 1
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "items with boolean schema (true)",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"items": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any array is valid",
|
||||
"data": [
|
||||
1,
|
||||
"foo",
|
||||
true
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty array is valid",
|
||||
"data": [],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "items with boolean schema (false)",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"items": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any non-empty array is invalid",
|
||||
"data": [
|
||||
1,
|
||||
"foo",
|
||||
true
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty array is valid",
|
||||
"data": [],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "items and subitems",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$defs": {
|
||||
"item": {
|
||||
"type": "array",
|
||||
"items": false,
|
||||
"prefixItems": [
|
||||
{
|
||||
"$ref": "#/$defs/sub-item"
|
||||
},
|
||||
{
|
||||
"$ref": "#/$defs/sub-item"
|
||||
}
|
||||
]
|
||||
},
|
||||
"sub-item": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"foo"
|
||||
]
|
||||
}
|
||||
},
|
||||
"type": "array",
|
||||
"items": false,
|
||||
"prefixItems": [
|
||||
{
|
||||
"$ref": "#/$defs/item"
|
||||
},
|
||||
{
|
||||
"$ref": "#/$defs/item"
|
||||
},
|
||||
{
|
||||
"$ref": "#/$defs/item"
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid items",
|
||||
"data": [
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
]
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "too many items",
|
||||
"data": [
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
]
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "too many sub-items",
|
||||
"data": [
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
]
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "wrong item",
|
||||
"data": [
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
]
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "wrong sub-item",
|
||||
"data": [
|
||||
[
|
||||
{},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
]
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "fewer items is invalid",
|
||||
"data": [
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
]
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "nested items",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "number"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid nested array",
|
||||
"data": [
|
||||
[
|
||||
[
|
||||
[
|
||||
1
|
||||
]
|
||||
],
|
||||
[
|
||||
[
|
||||
2
|
||||
],
|
||||
[
|
||||
3
|
||||
]
|
||||
]
|
||||
],
|
||||
[
|
||||
[
|
||||
[
|
||||
4
|
||||
],
|
||||
[
|
||||
5
|
||||
],
|
||||
[
|
||||
6
|
||||
]
|
||||
]
|
||||
]
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "nested array with invalid type",
|
||||
"data": [
|
||||
[
|
||||
[
|
||||
[
|
||||
"1"
|
||||
]
|
||||
],
|
||||
[
|
||||
[
|
||||
2
|
||||
],
|
||||
[
|
||||
3
|
||||
]
|
||||
]
|
||||
],
|
||||
[
|
||||
[
|
||||
[
|
||||
4
|
||||
],
|
||||
[
|
||||
5
|
||||
],
|
||||
[
|
||||
6
|
||||
]
|
||||
]
|
||||
]
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "not deep enough",
|
||||
"data": [
|
||||
[
|
||||
[
|
||||
1
|
||||
],
|
||||
[
|
||||
2
|
||||
],
|
||||
[
|
||||
3
|
||||
]
|
||||
],
|
||||
[
|
||||
[
|
||||
4
|
||||
],
|
||||
[
|
||||
5
|
||||
],
|
||||
[
|
||||
6
|
||||
]
|
||||
]
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "prefixItems with no additional items allowed",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"prefixItems": [
|
||||
{},
|
||||
{},
|
||||
{}
|
||||
],
|
||||
"items": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty array",
|
||||
"data": [],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "fewer number of items present (1)",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "fewer number of items present (2)",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "equal number of items present",
|
||||
"data": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "additional items are not permitted",
|
||||
"data": [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "items does not look in applicators, valid case",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"prefixItems": [
|
||||
{
|
||||
"minimum": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"items": {
|
||||
"minimum": 5
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "prefixItems in allOf does not constrain items, invalid case",
|
||||
"data": [
|
||||
3,
|
||||
5
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "prefixItems in allOf does not constrain items, valid case",
|
||||
"data": [
|
||||
5,
|
||||
5
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "prefixItems validation adjusts the starting index for items",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"prefixItems": [
|
||||
{
|
||||
"type": "string"
|
||||
}
|
||||
],
|
||||
"items": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid items",
|
||||
"data": [
|
||||
"x",
|
||||
2,
|
||||
3
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "wrong type of second item",
|
||||
"data": [
|
||||
"x",
|
||||
"y"
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "items with heterogeneous array",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"prefixItems": [
|
||||
{}
|
||||
],
|
||||
"items": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "heterogeneous invalid instance",
|
||||
"data": [
|
||||
"foo",
|
||||
"bar",
|
||||
37
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "valid instance",
|
||||
"data": [
|
||||
null
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "items with null instance elements",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"items": {
|
||||
"type": "null"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "allows null elements",
|
||||
"data": [
|
||||
null
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows extra items (when items is false)",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"items": false,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra item is valid",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows extra properties for items",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"items": {
|
||||
"minimum": 5
|
||||
},
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid item is valid",
|
||||
"data": [
|
||||
5,
|
||||
6
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid item (less than min) is invalid even with extensible: true",
|
||||
"data": [
|
||||
4
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "array: simple extensible array",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "array",
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty array is valid",
|
||||
"data": [],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array with items is valid (extensible)",
|
||||
"data": [
|
||||
1,
|
||||
"foo"
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "array: strict array",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "array",
|
||||
"extensible": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty array is valid",
|
||||
"data": [],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array with items is invalid (strict)",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "array: items extensible",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"extensible": true
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty array is valid",
|
||||
"data": [],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array with items is valid (items explicitly allowed to be anything extensible)",
|
||||
"data": [
|
||||
1,
|
||||
"foo",
|
||||
{}
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "array: items strict",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"extensible": false
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty array is valid (empty objects)",
|
||||
"data": [
|
||||
{}
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array with strict object items is valid",
|
||||
"data": [
|
||||
{}
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array with invalid strict object items (extra property)",
|
||||
"data": [
|
||||
{
|
||||
"extra": 1
|
||||
}
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
163
tests/fixtures/maxContains.json
vendored
Normal file
163
tests/fixtures/maxContains.json
vendored
Normal file
@ -0,0 +1,163 @@
|
||||
[
|
||||
{
|
||||
"description": "maxContains without contains is ignored",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxContains": 1,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "one item valid against lone maxContains",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "two items still valid against lone maxContains",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxContains with contains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 1
|
||||
},
|
||||
"maxContains": 1,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty data",
|
||||
"data": [],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "all elements match, valid maxContains",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "all elements match, invalid maxContains",
|
||||
"data": [
|
||||
1,
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "some elements match, valid maxContains",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "some elements match, invalid maxContains",
|
||||
"data": [
|
||||
1,
|
||||
2,
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxContains with contains, value with a decimal",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 1
|
||||
},
|
||||
"maxContains": 1.0,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "one element matches, valid maxContains",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too many elements match, invalid maxContains",
|
||||
"data": [
|
||||
1,
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minContains < maxContains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 1
|
||||
},
|
||||
"minContains": 1,
|
||||
"maxContains": 3,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "actual < minContains < maxContains",
|
||||
"data": [],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "minContains < actual < maxContains",
|
||||
"data": [
|
||||
1,
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "minContains < maxContains < actual",
|
||||
"data": [
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows non-matching items in maxContains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 1
|
||||
},
|
||||
"maxContains": 1,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra items disregarded for maxContains",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
86
tests/fixtures/maxItems.json
vendored
Normal file
86
tests/fixtures/maxItems.json
vendored
Normal file
@ -0,0 +1,86 @@
|
||||
[
|
||||
{
|
||||
"description": "maxItems validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxItems": 2,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "shorter is valid",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "exact length is valid",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too long is invalid",
|
||||
"data": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "ignores non-arrays",
|
||||
"data": "foobar",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxItems validation with a decimal",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxItems": 2.0,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "shorter is valid",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too long is invalid",
|
||||
"data": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows extra items in maxItems (but counted)",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxItems": 2,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra item counted towards maxItems",
|
||||
"data": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
55
tests/fixtures/maxLength.json
vendored
Normal file
55
tests/fixtures/maxLength.json
vendored
Normal file
@ -0,0 +1,55 @@
|
||||
[
|
||||
{
|
||||
"description": "maxLength validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxLength": 2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "shorter is valid",
|
||||
"data": "f",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "exact length is valid",
|
||||
"data": "fo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too long is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "ignores non-strings",
|
||||
"data": 100,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "two graphemes is long enough",
|
||||
"data": "\uD83D\uDCA9\uD83D\uDCA9",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxLength validation with a decimal",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxLength": 2.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "shorter is valid",
|
||||
"data": "f",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too long is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
129
tests/fixtures/maxProperties.json
vendored
Normal file
129
tests/fixtures/maxProperties.json
vendored
Normal file
@ -0,0 +1,129 @@
|
||||
[
|
||||
{
|
||||
"description": "maxProperties validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxProperties": 2,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "shorter is valid",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "exact length is valid",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too long is invalid",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2,
|
||||
"baz": 3
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "ignores arrays",
|
||||
"data": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "ignores strings",
|
||||
"data": "foobar",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "ignores other non-objects",
|
||||
"data": 12,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxProperties validation with a decimal",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxProperties": 2.0,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "shorter is valid",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too long is invalid",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2,
|
||||
"baz": 3
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxProperties = 0 means the object is empty",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxProperties": 0,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "no properties is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "one property is invalid",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows extra properties in maxProperties (though maxProperties still counts them!)",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxProperties": 2,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra property is counted towards maxProperties",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2,
|
||||
"baz": 3
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "extra property is valid if below maxProperties",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
60
tests/fixtures/maximum.json
vendored
Normal file
60
tests/fixtures/maximum.json
vendored
Normal file
@ -0,0 +1,60 @@
|
||||
[
|
||||
{
|
||||
"description": "maximum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maximum": 3.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "below the maximum is valid",
|
||||
"data": 2.6,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point is valid",
|
||||
"data": 3.0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "above the maximum is invalid",
|
||||
"data": 3.5,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "ignores non-numbers",
|
||||
"data": "x",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maximum validation with unsigned integer",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maximum": 300
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "below the maximum is invalid",
|
||||
"data": 299.97,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point integer is valid",
|
||||
"data": 300,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point float is valid",
|
||||
"data": 300.00,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "above the maximum is invalid",
|
||||
"data": 300.5,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
226
tests/fixtures/merge.json
vendored
Normal file
226
tests/fixtures/merge.json
vendored
Normal file
@ -0,0 +1,226 @@
|
||||
[
|
||||
{
|
||||
"description": "merging: properties accumulate",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$defs": {
|
||||
"base": {
|
||||
"properties": {
|
||||
"base_prop": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"$ref": "#/$defs/base",
|
||||
"properties": {
|
||||
"child_prop": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid with both properties",
|
||||
"data": {
|
||||
"base_prop": "a",
|
||||
"child_prop": "b"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid when base property has wrong type",
|
||||
"data": {
|
||||
"base_prop": 1,
|
||||
"child_prop": "b"
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "TYPE_MISMATCH",
|
||||
"path": "/base_prop"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "merging: required fields accumulate",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$defs": {
|
||||
"base": {
|
||||
"properties": {
|
||||
"a": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"a"
|
||||
]
|
||||
}
|
||||
},
|
||||
"$ref": "#/$defs/base",
|
||||
"properties": {
|
||||
"b": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"b"
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid when both present",
|
||||
"data": {
|
||||
"a": "ok",
|
||||
"b": "ok"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid when base required missing",
|
||||
"data": {
|
||||
"b": "ok"
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "REQUIRED_FIELD_MISSING",
|
||||
"path": "/a"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "invalid when child required missing",
|
||||
"data": {
|
||||
"a": "ok"
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "REQUIRED_FIELD_MISSING",
|
||||
"path": "/b"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "merging: dependencies accumulate",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$defs": {
|
||||
"base": {
|
||||
"properties": {
|
||||
"trigger": {
|
||||
"type": "string"
|
||||
},
|
||||
"base_dep": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"trigger": [
|
||||
"base_dep"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"$ref": "#/$defs/base",
|
||||
"properties": {
|
||||
"child_dep": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"trigger": [
|
||||
"child_dep"
|
||||
]
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid with all deps",
|
||||
"data": {
|
||||
"trigger": "go",
|
||||
"base_dep": "ok",
|
||||
"child_dep": "ok"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid missing base dep",
|
||||
"data": {
|
||||
"trigger": "go",
|
||||
"child_dep": "ok"
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "DEPENDENCY_FAILED",
|
||||
"path": "/base_dep"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "invalid missing child dep",
|
||||
"data": {
|
||||
"trigger": "go",
|
||||
"base_dep": "ok"
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "DEPENDENCY_FAILED",
|
||||
"path": "/child_dep"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "merging: form and display do NOT merge",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$defs": {
|
||||
"base": {
|
||||
"properties": {
|
||||
"a": {
|
||||
"type": "string"
|
||||
},
|
||||
"b": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"form": [
|
||||
"a",
|
||||
"b"
|
||||
]
|
||||
}
|
||||
},
|
||||
"$ref": "#/$defs/base",
|
||||
"properties": {
|
||||
"c": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"form": [
|
||||
"c"
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "child schema validation",
|
||||
"data": {
|
||||
"a": "ok",
|
||||
"b": "ok",
|
||||
"c": "ok"
|
||||
},
|
||||
"valid": true,
|
||||
"comment": "Verifies validator handles the unmerged metadata correctly (ignores it or handles replacement)"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
325
tests/fixtures/minContains.json
vendored
Normal file
325
tests/fixtures/minContains.json
vendored
Normal file
@ -0,0 +1,325 @@
|
||||
[
|
||||
{
|
||||
"description": "minContains without contains is ignored",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minContains": 1,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "one item valid against lone minContains",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "zero items still valid against lone minContains",
|
||||
"data": [],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minContains=1 with contains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 1
|
||||
},
|
||||
"minContains": 1,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty data",
|
||||
"data": [],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "no elements match",
|
||||
"data": [
|
||||
2
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "single element matches, valid minContains",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "some elements match, valid minContains",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "all elements match, valid minContains",
|
||||
"data": [
|
||||
1,
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minContains=2 with contains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 1
|
||||
},
|
||||
"minContains": 2,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty data",
|
||||
"data": [],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "all elements match, invalid minContains",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "some elements match, invalid minContains",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "all elements match, valid minContains (exactly as needed)",
|
||||
"data": [
|
||||
1,
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "all elements match, valid minContains (more than needed)",
|
||||
"data": [
|
||||
1,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "some elements match, valid minContains",
|
||||
"data": [
|
||||
1,
|
||||
2,
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minContains=2 with contains with a decimal value",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 1
|
||||
},
|
||||
"minContains": 2.0,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "one element matches, invalid minContains",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "both elements match, valid minContains",
|
||||
"data": [
|
||||
1,
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxContains = minContains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 1
|
||||
},
|
||||
"maxContains": 2,
|
||||
"minContains": 2,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty data",
|
||||
"data": [],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "all elements match, invalid minContains",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "all elements match, invalid maxContains",
|
||||
"data": [
|
||||
1,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "all elements match, valid maxContains and minContains",
|
||||
"data": [
|
||||
1,
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxContains < minContains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 1
|
||||
},
|
||||
"maxContains": 1,
|
||||
"minContains": 3,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty data",
|
||||
"data": [],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "invalid minContains",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "invalid maxContains",
|
||||
"data": [
|
||||
1,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "invalid maxContains and minContains",
|
||||
"data": [
|
||||
1,
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minContains = 0",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 1
|
||||
},
|
||||
"minContains": 0,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty data",
|
||||
"data": [],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "minContains = 0 makes contains always pass",
|
||||
"data": [
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minContains = 0 with maxContains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 1
|
||||
},
|
||||
"minContains": 0,
|
||||
"maxContains": 1,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty data",
|
||||
"data": [],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "not more than maxContains",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too many",
|
||||
"data": [
|
||||
1,
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows non-matching items in minContains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"const": 1
|
||||
},
|
||||
"minContains": 1,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra items disregarded for minContains",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
77
tests/fixtures/minItems.json
vendored
Normal file
77
tests/fixtures/minItems.json
vendored
Normal file
@ -0,0 +1,77 @@
|
||||
[
|
||||
{
|
||||
"description": "minItems validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minItems": 1,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "longer is valid",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "exact length is valid",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too short is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "ignores non-arrays",
|
||||
"data": "",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minItems validation with a decimal",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minItems": 1.0,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "longer is valid",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too short is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows extra items in minItems",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minItems": 1,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra item counted towards minItems",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
55
tests/fixtures/minLength.json
vendored
Normal file
55
tests/fixtures/minLength.json
vendored
Normal file
@ -0,0 +1,55 @@
|
||||
[
|
||||
{
|
||||
"description": "minLength validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minLength": 2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "longer is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "exact length is valid",
|
||||
"data": "fo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too short is invalid",
|
||||
"data": "f",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "ignores non-strings",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "one grapheme is not long enough",
|
||||
"data": "\uD83D\uDCA9",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minLength validation with a decimal",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minLength": 2.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "longer is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too short is invalid",
|
||||
"data": "f",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
87
tests/fixtures/minProperties.json
vendored
Normal file
87
tests/fixtures/minProperties.json
vendored
Normal file
@ -0,0 +1,87 @@
|
||||
[
|
||||
{
|
||||
"description": "minProperties validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minProperties": 1,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "longer is valid",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "exact length is valid",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too short is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "ignores arrays",
|
||||
"data": [],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "ignores strings",
|
||||
"data": "",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "ignores other non-objects",
|
||||
"data": 12,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minProperties validation with a decimal",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minProperties": 1.0,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "longer is valid",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too short is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows extra properties in minProperties",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minProperties": 1,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra property counts towards minProperties",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
75
tests/fixtures/minimum.json
vendored
Normal file
75
tests/fixtures/minimum.json
vendored
Normal file
@ -0,0 +1,75 @@
|
||||
[
|
||||
{
|
||||
"description": "minimum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minimum": 1.1
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "above the minimum is valid",
|
||||
"data": 2.6,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point is valid",
|
||||
"data": 1.1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "below the minimum is invalid",
|
||||
"data": 0.6,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "ignores non-numbers",
|
||||
"data": "x",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minimum validation with signed integer",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minimum": -2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "negative above the minimum is valid",
|
||||
"data": -1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "positive above the minimum is valid",
|
||||
"data": 0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point is valid",
|
||||
"data": -2,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point with float is valid",
|
||||
"data": -2.0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "float below the minimum is invalid",
|
||||
"data": -2.0001,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "int below the minimum is invalid",
|
||||
"data": -3,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "ignores non-numbers",
|
||||
"data": "x",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
84
tests/fixtures/multipleOf.json
vendored
Normal file
84
tests/fixtures/multipleOf.json
vendored
Normal file
@ -0,0 +1,84 @@
|
||||
[
|
||||
{
|
||||
"description": "by int",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"multipleOf": 2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "int by int",
|
||||
"data": 10,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "int by int fail",
|
||||
"data": 7,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "ignores non-numbers",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "by number",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"multipleOf": 1.5
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "zero is multiple of anything",
|
||||
"data": 0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "4.5 is multiple of 1.5",
|
||||
"data": 4.5,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "35 is not multiple of 1.5",
|
||||
"data": 35,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "by small number",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"multipleOf": 0.0001
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "0.0075 is multiple of 0.0001",
|
||||
"data": 0.0075,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "0.00751 is not multiple of 0.0001",
|
||||
"data": 0.00751,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "small multiple of large integer",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "integer",
|
||||
"multipleOf": 1e-8
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any integer is a multiple of 1e-8",
|
||||
"data": 12391239123,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
398
tests/fixtures/not.json
vendored
Normal file
398
tests/fixtures/not.json
vendored
Normal file
@ -0,0 +1,398 @@
|
||||
[
|
||||
{
|
||||
"description": "not",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "allowed",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "disallowed",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "not multiple types",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": {
|
||||
"type": [
|
||||
"integer",
|
||||
"boolean"
|
||||
]
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "mismatch",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "other mismatch",
|
||||
"data": true,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "not more complex schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "match",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "other match",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "mismatch",
|
||||
"data": {
|
||||
"foo": "bar"
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "forbidden property",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"foo": {
|
||||
"not": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "property present",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty object is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "forbid everything with empty schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": {}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "number is invalid",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "string is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "boolean true is invalid",
|
||||
"data": true,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "boolean false is invalid",
|
||||
"data": false,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "null is invalid",
|
||||
"data": null,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "object is invalid",
|
||||
"data": {
|
||||
"foo": "bar"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty object is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "array is invalid",
|
||||
"data": [
|
||||
"foo"
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "forbid everything with boolean schema true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "number is invalid",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "string is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "boolean true is invalid",
|
||||
"data": true,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "boolean false is invalid",
|
||||
"data": false,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "null is invalid",
|
||||
"data": null,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "object is invalid",
|
||||
"data": {
|
||||
"foo": "bar"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty object is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "array is invalid",
|
||||
"data": [
|
||||
"foo"
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allow everything with boolean schema false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": false,
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "number is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "string is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boolean true is valid",
|
||||
"data": true,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boolean false is valid",
|
||||
"data": false,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "null is valid",
|
||||
"data": null,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "object is valid",
|
||||
"data": {
|
||||
"foo": "bar"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty object is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array is valid",
|
||||
"data": [
|
||||
"foo"
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty array is valid",
|
||||
"data": [],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "double negation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": {
|
||||
"not": {}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: true allows extra properties in not",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": {
|
||||
"type": "integer"
|
||||
},
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra property is valid (not integer matches)",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "extensible: false (default) forbids extra properties in not",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra property is invalid due to strictness",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "property next to not (extensible: true)",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"bar": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"not": {
|
||||
"type": "integer"
|
||||
},
|
||||
"extensible": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra property allowed",
|
||||
"data": {
|
||||
"bar": "baz",
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "property next to not (extensible: false)",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"bar": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"not": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "extra property forbidden",
|
||||
"data": {
|
||||
"bar": "baz",
|
||||
"foo": 1
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "defined property allowed",
|
||||
"data": {
|
||||
"bar": "baz"
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
392
tests/fixtures/old/allOf.json
vendored
Normal file
392
tests/fixtures/old/allOf.json
vendored
Normal file
@ -0,0 +1,392 @@
|
||||
[
|
||||
{
|
||||
"description": "allOf",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"properties": {
|
||||
"bar": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"bar"
|
||||
]
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"foo"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "allOf",
|
||||
"data": {
|
||||
"foo": "baz",
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "mismatch second",
|
||||
"data": {
|
||||
"foo": "baz"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "mismatch first",
|
||||
"data": {
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "wrong type",
|
||||
"data": {
|
||||
"foo": "baz",
|
||||
"bar": "quux"
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with base schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"bar": {
|
||||
"type": "integer"
|
||||
},
|
||||
"baz": {},
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"bar"
|
||||
],
|
||||
"allOf": [
|
||||
{
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"foo"
|
||||
]
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"baz": {
|
||||
"type": "null"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"baz"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid",
|
||||
"data": {
|
||||
"foo": "quux",
|
||||
"bar": 2,
|
||||
"baz": null
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "mismatch base schema",
|
||||
"data": {
|
||||
"foo": "quux",
|
||||
"baz": null
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "mismatch first allOf",
|
||||
"data": {
|
||||
"bar": 2,
|
||||
"baz": null
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "mismatch second allOf",
|
||||
"data": {
|
||||
"foo": "quux",
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "mismatch both",
|
||||
"data": {
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf simple types",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"maximum": 30
|
||||
},
|
||||
{
|
||||
"minimum": 20
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid",
|
||||
"data": 25,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "mismatch one",
|
||||
"data": 35,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with boolean schemas, all true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
true,
|
||||
true
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with boolean schemas, some false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
true,
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with boolean schemas, all false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with one empty schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any data is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with two empty schemas",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{},
|
||||
{}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any data is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with the first empty schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{},
|
||||
{
|
||||
"type": "number"
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "number is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "string is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf with the last empty schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"type": "number"
|
||||
},
|
||||
{}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "number is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "string is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "nested allOf, to check validation semantics",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"allOf": [
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "null is valid",
|
||||
"data": null,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "anything non-null is invalid",
|
||||
"data": 123,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allOf combined with anyOf, oneOf",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"multipleOf": 2
|
||||
}
|
||||
],
|
||||
"anyOf": [
|
||||
{
|
||||
"multipleOf": 3
|
||||
}
|
||||
],
|
||||
"oneOf": [
|
||||
{
|
||||
"multipleOf": 5
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "allOf: false, anyOf: false, oneOf: false",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "allOf: false, anyOf: false, oneOf: true",
|
||||
"data": 5,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "allOf: false, anyOf: true, oneOf: false",
|
||||
"data": 3,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "allOf: false, anyOf: true, oneOf: true",
|
||||
"data": 15,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "allOf: true, anyOf: false, oneOf: false",
|
||||
"data": 2,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "allOf: true, anyOf: false, oneOf: true",
|
||||
"data": 10,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "allOf: true, anyOf: true, oneOf: false",
|
||||
"data": 6,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "allOf: true, anyOf: true, oneOf: true",
|
||||
"data": 30,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
120
tests/fixtures/old/anchor.json
vendored
Normal file
120
tests/fixtures/old/anchor.json
vendored
Normal file
@ -0,0 +1,120 @@
|
||||
[
|
||||
{
|
||||
"description": "Location-independent identifier",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$ref": "#foo",
|
||||
"$defs": {
|
||||
"A": {
|
||||
"$anchor": "foo",
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"data": 1,
|
||||
"description": "match",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"data": "a",
|
||||
"description": "mismatch",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "Location-independent identifier with absolute URI",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$ref": "http://localhost:1234/draft2020-12/bar#foo",
|
||||
"$defs": {
|
||||
"A": {
|
||||
"$id": "http://localhost:1234/draft2020-12/bar",
|
||||
"$anchor": "foo",
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"data": 1,
|
||||
"description": "match",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"data": "a",
|
||||
"description": "mismatch",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "Location-independent identifier with base URI change in subschema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "http://localhost:1234/draft2020-12/root",
|
||||
"$ref": "http://localhost:1234/draft2020-12/nested.json#foo",
|
||||
"$defs": {
|
||||
"A": {
|
||||
"$id": "nested.json",
|
||||
"$defs": {
|
||||
"B": {
|
||||
"$anchor": "foo",
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"data": 1,
|
||||
"description": "match",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"data": "a",
|
||||
"description": "mismatch",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "same $anchor with different base uri",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "http://localhost:1234/draft2020-12/foobar",
|
||||
"$defs": {
|
||||
"A": {
|
||||
"$id": "child1",
|
||||
"allOf": [
|
||||
{
|
||||
"$id": "child2",
|
||||
"$anchor": "my_anchor",
|
||||
"type": "number"
|
||||
},
|
||||
{
|
||||
"$anchor": "my_anchor",
|
||||
"type": "string"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"$ref": "child1#my_anchor"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "$ref resolves to /$defs/A/allOf/1",
|
||||
"data": "a",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "$ref does not resolve to /$defs/A/allOf/0",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
164
tests/fixtures/old/anyOf.json
vendored
Normal file
164
tests/fixtures/old/anyOf.json
vendored
Normal file
@ -0,0 +1,164 @@
|
||||
[
|
||||
{
|
||||
"description": "anyOf with boolean schemas, all true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
true,
|
||||
true
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "anyOf with boolean schemas, some true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
true,
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "anyOf with boolean schemas, all false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "anyOf complex types",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
{
|
||||
"properties": {
|
||||
"bar": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"bar"
|
||||
]
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"foo"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "first anyOf valid (complex)",
|
||||
"data": {
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "second anyOf valid (complex)",
|
||||
"data": {
|
||||
"foo": "baz"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "both anyOf valid (complex)",
|
||||
"data": {
|
||||
"foo": "baz",
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "neither anyOf valid (complex)",
|
||||
"data": {
|
||||
"foo": 2,
|
||||
"bar": "quux"
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "anyOf with one empty schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "number"
|
||||
},
|
||||
{}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "string is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "number is valid",
|
||||
"data": 123,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "nested anyOf, to check validation semantics",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"anyOf": [
|
||||
{
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "null is valid",
|
||||
"data": null,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "anything non-null is invalid",
|
||||
"data": 123,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
104
tests/fixtures/old/boolean_schema.json
vendored
Normal file
104
tests/fixtures/old/boolean_schema.json
vendored
Normal file
@ -0,0 +1,104 @@
|
||||
[
|
||||
{
|
||||
"description": "boolean schema 'true'",
|
||||
"schema": true,
|
||||
"tests": [
|
||||
{
|
||||
"description": "number is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "string is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boolean true is valid",
|
||||
"data": true,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boolean false is valid",
|
||||
"data": false,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "null is valid",
|
||||
"data": null,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "object is valid",
|
||||
"data": {"foo": "bar"},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty object is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array is valid",
|
||||
"data": ["foo"],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty array is valid",
|
||||
"data": [],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "boolean schema 'false'",
|
||||
"schema": false,
|
||||
"tests": [
|
||||
{
|
||||
"description": "number is invalid",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "string is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "boolean true is invalid",
|
||||
"data": true,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "boolean false is invalid",
|
||||
"data": false,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "null is invalid",
|
||||
"data": null,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "object is invalid",
|
||||
"data": {"foo": "bar"},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty object is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "array is invalid",
|
||||
"data": ["foo"],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
62
tests/fixtures/old/cache.json
vendored
Normal file
62
tests/fixtures/old/cache.json
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
[
|
||||
{
|
||||
"description": "handling of non-existent schemas",
|
||||
"enums": [],
|
||||
"types": [],
|
||||
"puncs": [],
|
||||
"tests": [
|
||||
{
|
||||
"description": "validate against non-existent schema",
|
||||
"schema_id": "non_existent_schema",
|
||||
"data": {
|
||||
"foo": "bar"
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "SCHEMA_NOT_FOUND",
|
||||
"path": "",
|
||||
"message_contains": "Schema 'non_existent_schema' not found"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "invalid schema caching",
|
||||
"enums": [],
|
||||
"types": [],
|
||||
"puncs": [
|
||||
{
|
||||
"name": "invalid_punc",
|
||||
"public": false,
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "invalid_punc.request",
|
||||
"type": [
|
||||
"invalid_type_value"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"description": "cache returns errors for invalid types",
|
||||
"action": "cache",
|
||||
"data": {},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "ENUM_VIOLATED",
|
||||
"path": ""
|
||||
},
|
||||
{
|
||||
"code": "SCHEMA_PARSE_FAILED",
|
||||
"path": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
481
tests/fixtures/old/const.json
vendored
Executable file
481
tests/fixtures/old/const.json
vendored
Executable file
@ -0,0 +1,481 @@
|
||||
[
|
||||
{
|
||||
"description": "const validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": 2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "same value is valid",
|
||||
"data": 2,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "another value is invalid",
|
||||
"data": 5,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "another type is invalid",
|
||||
"data": "a",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with object",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": {
|
||||
"foo": "bar",
|
||||
"baz": "bax"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "same object is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "same object with different property order is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "another object is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "another type is invalid",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with array",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": [
|
||||
{
|
||||
"foo": "bar"
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "same array is valid",
|
||||
"data": [
|
||||
{
|
||||
"foo": "bar"
|
||||
}
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "another array item is invalid",
|
||||
"data": [
|
||||
2
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with null",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": null
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "null is valid",
|
||||
"data": null,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "not null is invalid",
|
||||
"data": 0,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with false does not match 0",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "false is valid",
|
||||
"data": false,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "integer zero is invalid",
|
||||
"data": 0,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "float zero is invalid",
|
||||
"data": 0.0,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with true does not match 1",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "true is valid",
|
||||
"data": true,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "integer one is invalid",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "float one is invalid",
|
||||
"data": 1.0,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with [false] does not match [0]",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "[false] is valid",
|
||||
"data": [
|
||||
false
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "[0] is invalid",
|
||||
"data": [
|
||||
0
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "[0.0] is invalid",
|
||||
"data": [
|
||||
0.0
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with [true] does not match [1]",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": [
|
||||
true
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "[true] is valid",
|
||||
"data": [
|
||||
true
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "[1] is invalid",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "[1.0] is invalid",
|
||||
"data": [
|
||||
1.0
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with {\"a\": false} does not match {\"a\": 0}",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": {
|
||||
"a": false
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "{\"a\": false} is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "{\"a\": 0} is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "{\"a\": 0.0} is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with {\"a\": true} does not match {\"a\": 1}",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": {
|
||||
"a": true
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "{\"a\": true} is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "{\"a\": 1} is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "{\"a\": 1.0} is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with 0 does not match other zero-like types",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": 0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "false is invalid",
|
||||
"data": false,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "integer zero is valid",
|
||||
"data": 0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "float zero is valid",
|
||||
"data": 0.0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty object is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty string is invalid",
|
||||
"data": "",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with 1 does not match true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": 1
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "true is invalid",
|
||||
"data": true,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "integer one is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "float one is valid",
|
||||
"data": 1.0,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "const with -2.0 matches integer and float types",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": -2.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "integer -2 is valid",
|
||||
"data": -2,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "integer 2 is invalid",
|
||||
"data": 2,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "float -2.0 is valid",
|
||||
"data": -2.0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "float 2.0 is invalid",
|
||||
"data": 2.0,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "float -2.00001 is invalid",
|
||||
"data": -2.00001,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "float and integers are equal up to 64-bit representation limits",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": 9007199254740992
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "integer is valid",
|
||||
"data": 9007199254740992,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "integer minus one is invalid",
|
||||
"data": 9007199254740991,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "float is valid",
|
||||
"data": 9007199254740992.0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "float minus one is invalid",
|
||||
"data": 9007199254740991.0,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "nul characters in strings",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": "hello\u0000there"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "match string with nul",
|
||||
"data": "hello\u0000there",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "do not match string lacking nul",
|
||||
"data": "hellothere",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "characters with the same visual representation but different codepoint",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": "\u03bc",
|
||||
"$comment": "U+03BC"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "character uses the same codepoint",
|
||||
"data": "\u03bc",
|
||||
"comment": "U+03BC",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "character looks the same but uses a different codepoint",
|
||||
"data": "\u00b5",
|
||||
"comment": "U+00B5",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "characters with the same visual representation, but different number of codepoints",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": "\u00e4",
|
||||
"$comment": "U+00E4"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "character uses the same codepoint",
|
||||
"data": "\u00e4",
|
||||
"comment": "U+00E4",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "character looks the same but uses combining marks",
|
||||
"data": "a\u0308",
|
||||
"comment": "a, U+0308",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "zero fraction",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": 2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "with fraction",
|
||||
"data": 2.0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "without fraction",
|
||||
"data": 2,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
176
tests/fixtures/old/contains.json
vendored
Normal file
176
tests/fixtures/old/contains.json
vendored
Normal file
@ -0,0 +1,176 @@
|
||||
[
|
||||
{
|
||||
"description": "contains keyword validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {"minimum": 5}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "array with item matching schema (5) is valid",
|
||||
"data": [3, 4, 5],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array with item matching schema (6) is valid",
|
||||
"data": [3, 4, 6],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array with two items matching schema (5, 6) is valid",
|
||||
"data": [3, 4, 5, 6],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array without items matching schema is invalid",
|
||||
"data": [2, 3, 4],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "not array is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "contains keyword with const keyword",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": { "const": 5 }
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "array with item 5 is valid",
|
||||
"data": [3, 4, 5],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array with two items 5 is valid",
|
||||
"data": [3, 4, 5, 5],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array without item 5 is invalid",
|
||||
"data": [1, 2, 3, 4],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "contains keyword with boolean schema true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any non-empty array is valid",
|
||||
"data": ["foo"],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "contains keyword with boolean schema false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any non-empty array is invalid",
|
||||
"data": ["foo"],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "non-arrays are valid",
|
||||
"data": "contains does not apply to strings",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "items + contains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"items": { "multipleOf": 2 },
|
||||
"contains": { "multipleOf": 3 }
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "matches items, does not match contains",
|
||||
"data": [ 2, 4, 8 ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "does not match items, matches contains",
|
||||
"data": [ 3, 6, 9 ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "matches both items and contains",
|
||||
"data": [ 6, 12 ],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "matches neither items nor contains",
|
||||
"data": [ 1, 5 ],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "contains with false if subschema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"if": false,
|
||||
"else": true
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any non-empty array is valid",
|
||||
"data": ["foo"],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "contains with null instance elements",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {
|
||||
"type": "null"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "allows null items",
|
||||
"data": [ null ],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
121
tests/fixtures/old/content.json
vendored
Normal file
121
tests/fixtures/old/content.json
vendored
Normal file
@ -0,0 +1,121 @@
|
||||
[
|
||||
{
|
||||
"description": "validation of string-encoded content based on media type",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contentMediaType": "application/json"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "a valid JSON document",
|
||||
"data": "{\"foo\": \"bar\"}",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an invalid JSON document; validates true",
|
||||
"data": "{:}",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "validation of binary string-encoding",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contentEncoding": "base64"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "a valid base64 string",
|
||||
"data": "eyJmb28iOiAiYmFyIn0K",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an invalid base64 string (% is not a valid character); validates true",
|
||||
"data": "eyJmb28iOi%iYmFyIn0K",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "validation of binary-encoded media type documents",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contentMediaType": "application/json",
|
||||
"contentEncoding": "base64"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "a valid base64-encoded JSON document",
|
||||
"data": "eyJmb28iOiAiYmFyIn0K",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "a validly-encoded invalid JSON document; validates true",
|
||||
"data": "ezp9Cg==",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an invalid base64 string that is valid JSON; validates true",
|
||||
"data": "{}",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "validation of binary-encoded media type documents with schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contentMediaType": "application/json",
|
||||
"contentEncoding": "base64",
|
||||
"contentSchema": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"foo"
|
||||
],
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "a valid base64-encoded JSON document",
|
||||
"data": "eyJmb28iOiAiYmFyIn0K",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "another valid base64-encoded JSON document",
|
||||
"data": "eyJib28iOiAyMCwgImZvbyI6ICJiYXoifQ==",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an invalid base64-encoded JSON document; validates true",
|
||||
"data": "eyJib28iOiAyMH0=",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an empty object as a base64-encoded JSON document; validates true",
|
||||
"data": "e30=",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an empty array as a base64-encoded JSON document",
|
||||
"data": "W10=",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "a validly-encoded invalid JSON document; validates true",
|
||||
"data": "ezp9Cg==",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an invalid base64 string that is valid JSON; validates true",
|
||||
"data": "{}",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
82
tests/fixtures/old/default.json
vendored
Normal file
82
tests/fixtures/old/default.json
vendored
Normal file
@ -0,0 +1,82 @@
|
||||
[
|
||||
{
|
||||
"description": "invalid type for default",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "integer",
|
||||
"default": []
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid when property is specified",
|
||||
"data": {"foo": 13},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "still valid when the invalid default is used",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "invalid string value for default",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"bar": {
|
||||
"type": "string",
|
||||
"minLength": 4,
|
||||
"default": "bad"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid when property is specified",
|
||||
"data": {"bar": "good"},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "still valid when the invalid default is used",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "the default keyword does not do anything if the property is missing",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"alpha": {
|
||||
"type": "number",
|
||||
"maximum": 3,
|
||||
"default": 5
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "an explicit property value is checked against maximum (passing)",
|
||||
"data": { "alpha": 1 },
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an explicit property value is checked against maximum (failing)",
|
||||
"data": { "alpha": 5 },
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "missing properties are not filled in with the default",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
76
tests/fixtures/old/defs.json
vendored
Normal file
76
tests/fixtures/old/defs.json
vendored
Normal file
@ -0,0 +1,76 @@
|
||||
[
|
||||
{
|
||||
"description": "valid definition schema (using $defs)",
|
||||
"schema": {
|
||||
"$defs": {
|
||||
"foo": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"$ref": "#/$defs/foo"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid definition",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid definition",
|
||||
"data": "a",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "valid definition schema (using definitions for compat)",
|
||||
"schema": {
|
||||
"definitions": {
|
||||
"foo": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"$ref": "#/definitions/foo"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid definition",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid definition",
|
||||
"data": "a",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "nested definitions",
|
||||
"schema": {
|
||||
"$defs": {
|
||||
"foo": {
|
||||
"$defs": {
|
||||
"bar": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"$ref": "#/$defs/foo/$defs/bar"
|
||||
}
|
||||
},
|
||||
"$ref": "#/$defs/foo"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid nested definition",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid nested definition",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
325
tests/fixtures/old/dependencies.json
vendored
Normal file
325
tests/fixtures/old/dependencies.json
vendored
Normal file
@ -0,0 +1,325 @@
|
||||
[
|
||||
{
|
||||
"description": "basic field dependencies",
|
||||
"enums": [],
|
||||
"types": [],
|
||||
"puncs": [
|
||||
{
|
||||
"name": "dependency_split_test",
|
||||
"public": false,
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "dependency_split_test.request",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"creating": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"kind": {
|
||||
"type": "string"
|
||||
},
|
||||
"description": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"creating": [
|
||||
"name",
|
||||
"kind"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"description": "missing both dependent fields",
|
||||
"schema_id": "dependency_split_test.request",
|
||||
"data": {
|
||||
"creating": true,
|
||||
"description": "desc"
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "DEPENDENCY_FAILED",
|
||||
"path": "/name"
|
||||
},
|
||||
{
|
||||
"code": "DEPENDENCY_FAILED",
|
||||
"path": "/kind"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "missing one dependent field",
|
||||
"schema_id": "dependency_split_test.request",
|
||||
"data": {
|
||||
"creating": true,
|
||||
"name": "My Account"
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "DEPENDENCY_FAILED",
|
||||
"path": "/kind"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "no triggering field present",
|
||||
"schema_id": "dependency_split_test.request",
|
||||
"data": {
|
||||
"description": "desc"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "triggering field is false but present - dependencies apply",
|
||||
"schema_id": "dependency_split_test.request",
|
||||
"data": {
|
||||
"creating": false,
|
||||
"description": "desc"
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "DEPENDENCY_FAILED",
|
||||
"path": "/name"
|
||||
},
|
||||
{
|
||||
"code": "DEPENDENCY_FAILED",
|
||||
"path": "/kind"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "nested dependencies in array items",
|
||||
"enums": [],
|
||||
"types": [],
|
||||
"puncs": [
|
||||
{
|
||||
"name": "nested_dep_test",
|
||||
"public": false,
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "nested_dep_test.request",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string"
|
||||
},
|
||||
"creating": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"kind": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"id"
|
||||
],
|
||||
"dependencies": {
|
||||
"creating": [
|
||||
"name",
|
||||
"kind"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"items"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"description": "violations in array items",
|
||||
"schema_id": "nested_dep_test.request",
|
||||
"data": {
|
||||
"items": [
|
||||
{
|
||||
"id": "item1",
|
||||
"creating": true
|
||||
},
|
||||
{
|
||||
"id": "item2",
|
||||
"creating": true,
|
||||
"name": "Item 2"
|
||||
}
|
||||
]
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "DEPENDENCY_FAILED",
|
||||
"path": "/items/0/name"
|
||||
},
|
||||
{
|
||||
"code": "DEPENDENCY_FAILED",
|
||||
"path": "/items/0/kind"
|
||||
},
|
||||
{
|
||||
"code": "DEPENDENCY_FAILED",
|
||||
"path": "/items/1/kind"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "dependency merging across inheritance",
|
||||
"enums": [],
|
||||
"puncs": [],
|
||||
"types": [
|
||||
{
|
||||
"name": "entity",
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "entity",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
},
|
||||
"type": {
|
||||
"type": "string"
|
||||
},
|
||||
"created_by": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
},
|
||||
"creating": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"id",
|
||||
"type",
|
||||
"created_by"
|
||||
],
|
||||
"dependencies": {
|
||||
"creating": [
|
||||
"name"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "user",
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "user",
|
||||
"$ref": "entity",
|
||||
"properties": {
|
||||
"password": {
|
||||
"type": "string",
|
||||
"minLength": 8
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"creating": [
|
||||
"name"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "person",
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "person",
|
||||
"$ref": "user",
|
||||
"properties": {
|
||||
"first_name": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"last_name": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"creating": [
|
||||
"first_name",
|
||||
"last_name"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"description": "merged dependencies across inheritance",
|
||||
"schema_id": "person",
|
||||
"data": {
|
||||
"id": "550e8400-e29b-41d4-a716-446655440000",
|
||||
"type": "person",
|
||||
"created_by": "550e8400-e29b-41d4-a716-446655440001",
|
||||
"creating": true,
|
||||
"password": "securepass"
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "DEPENDENCY_FAILED",
|
||||
"path": "/name"
|
||||
},
|
||||
{
|
||||
"code": "DEPENDENCY_FAILED",
|
||||
"path": "/first_name"
|
||||
},
|
||||
{
|
||||
"code": "DEPENDENCY_FAILED",
|
||||
"path": "/last_name"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "partial dependency satisfaction across inheritance",
|
||||
"schema_id": "person",
|
||||
"data": {
|
||||
"id": "550e8400-e29b-41d4-a716-446655440000",
|
||||
"type": "person",
|
||||
"created_by": "550e8400-e29b-41d4-a716-446655440001",
|
||||
"creating": true,
|
||||
"password": "securepass",
|
||||
"name": "John Doe",
|
||||
"first_name": "John"
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "DEPENDENCY_FAILED",
|
||||
"path": "/last_name"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
141
tests/fixtures/old/dependentRequired.json
vendored
Normal file
141
tests/fixtures/old/dependentRequired.json
vendored
Normal file
@ -0,0 +1,141 @@
|
||||
[
|
||||
{
|
||||
"description": "single dependency",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"dependentRequired": {
|
||||
"bar": [
|
||||
"foo"
|
||||
]
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "neither",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "nondependant",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "with dependency",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "missing dependency",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "empty dependents",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"dependentRequired": {
|
||||
"bar": []
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty object",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "object with one property",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "non-object is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "multiple dependents required",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"dependentRequired": {
|
||||
"quux": [
|
||||
"foo",
|
||||
"bar"
|
||||
]
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "neither",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "nondependants",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "with dependencies",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "missing dependency",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "missing other dependency",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "missing both dependencies",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "dependencies with escaped characters",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"dependentRequired": {
|
||||
"foo\nbar": [
|
||||
"foo\rbar"
|
||||
],
|
||||
"foo\"bar": [
|
||||
"foo'bar"
|
||||
]
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "CRLF",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "quoted quotes",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "CRLF missing dependent",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "quoted quotes missing dependent",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
117
tests/fixtures/old/dependentSchemas.json
vendored
Normal file
117
tests/fixtures/old/dependentSchemas.json
vendored
Normal file
@ -0,0 +1,117 @@
|
||||
[
|
||||
{
|
||||
"description": "single dependency",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"dependentSchemas": {
|
||||
"bar": {
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "integer"
|
||||
},
|
||||
"bar": {
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "no dependency",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "wrong type",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "wrong type other",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "wrong type both",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "boolean subschemas",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"dependentSchemas": {
|
||||
"foo": true,
|
||||
"bar": false
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "object with property having schema true is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "object with property having schema false is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "object with both properties is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty object is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "dependencies with escaped characters",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"dependentSchemas": {
|
||||
"foo\tbar": {
|
||||
"minProperties": 4
|
||||
},
|
||||
"foo'bar": {
|
||||
"required": [
|
||||
"foo\"bar"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "quoted tab",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "quoted quote",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "quoted tab invalid under dependent schema",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "quoted quote invalid under dependent schema",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
121
tests/fixtures/old/dynamicRef.json
vendored
Normal file
121
tests/fixtures/old/dynamicRef.json
vendored
Normal file
@ -0,0 +1,121 @@
|
||||
[
|
||||
{
|
||||
"description": "Simple dynamicRef to dynamicAnchor in same schema",
|
||||
"schema": {
|
||||
"$id": "https://test.jspg.org/dynamic-ref/simple",
|
||||
"$dynamicAnchor": "root",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$dynamicRef": "#item"
|
||||
},
|
||||
"$defs": {
|
||||
"item": {
|
||||
"$dynamicAnchor": "item",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid string item",
|
||||
"data": [
|
||||
"foo"
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid number item",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "Dynamic scope resolution (generic list pattern)",
|
||||
"schema": {
|
||||
"$id": "https://test.jspg.org/dynamic-ref/override",
|
||||
"$dynamicAnchor": "root",
|
||||
"$ref": "https://test.jspg.org/dynamic-ref/generic-list",
|
||||
"$defs": {
|
||||
"generic-list": {
|
||||
"$id": "https://test.jspg.org/dynamic-ref/generic-list",
|
||||
"$dynamicAnchor": "list",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$dynamicRef": "#item"
|
||||
},
|
||||
"$defs": {
|
||||
"defaultItem": {
|
||||
"$dynamicAnchor": "item",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"override-item": {
|
||||
"$dynamicAnchor": "item",
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "integers valid (overridden)",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "strings invalid (overridden)",
|
||||
"data": [
|
||||
"a"
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "Dynamic scope resolution (no override uses default)",
|
||||
"schema": {
|
||||
"$id": "https://test.jspg.org/dynamic-ref/no-override",
|
||||
"$dynamicAnchor": "root",
|
||||
"$ref": "https://test.jspg.org/dynamic-ref/generic-list-2",
|
||||
"$defs": {
|
||||
"generic-list-2": {
|
||||
"$id": "https://test.jspg.org/dynamic-ref/generic-list-2",
|
||||
"$dynamicAnchor": "list",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$dynamicRef": "#item"
|
||||
},
|
||||
"$defs": {
|
||||
"defaultItem": {
|
||||
"$dynamicAnchor": "item",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "strings valid (default)",
|
||||
"data": [
|
||||
"a",
|
||||
"b"
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "integers invalid (default)",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
491
tests/fixtures/old/enum.json
vendored
Normal file
491
tests/fixtures/old/enum.json
vendored
Normal file
@ -0,0 +1,491 @@
|
||||
[
|
||||
{
|
||||
"description": "simple enum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "one of the enum is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "something else is invalid",
|
||||
"data": 4,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "heterogeneous enum-with-null validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
6,
|
||||
null
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "null is valid",
|
||||
"data": null,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "number is valid",
|
||||
"data": 6,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "something else is invalid",
|
||||
"data": "test",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enums in properties",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"foo": {
|
||||
"enum": [
|
||||
"foo"
|
||||
]
|
||||
},
|
||||
"bar": {
|
||||
"enum": [
|
||||
"bar"
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"bar"
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "both properties are valid",
|
||||
"data": {
|
||||
"foo": "foo",
|
||||
"bar": "bar"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "wrong foo value",
|
||||
"data": {
|
||||
"foo": "foot",
|
||||
"bar": "bar"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "wrong bar value",
|
||||
"data": {
|
||||
"foo": "foo",
|
||||
"bar": "bart"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "missing optional property is valid",
|
||||
"data": {
|
||||
"bar": "bar"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "missing required property is invalid",
|
||||
"data": {
|
||||
"foo": "foo"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "missing all properties is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with escaped characters",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
"foo\nbar",
|
||||
"foo\rbar"
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "member 1 is valid",
|
||||
"data": "foo\nbar",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "member 2 is valid",
|
||||
"data": "foo\rbar",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "another string is invalid",
|
||||
"data": "abc",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with false does not match 0",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "false is valid",
|
||||
"data": false,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "integer zero is invalid",
|
||||
"data": 0,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "float zero is invalid",
|
||||
"data": 0.0,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with [false] does not match [0]",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
[
|
||||
false
|
||||
]
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "[false] is valid",
|
||||
"data": [
|
||||
false
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "[0] is invalid",
|
||||
"data": [
|
||||
0
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "[0.0] is invalid",
|
||||
"data": [
|
||||
0.0
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with true does not match 1",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
true
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "true is valid",
|
||||
"data": true,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "integer one is invalid",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "float one is invalid",
|
||||
"data": 1.0,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with [true] does not match [1]",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
[
|
||||
true
|
||||
]
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "[true] is valid",
|
||||
"data": [
|
||||
true
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "[1] is invalid",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "[1.0] is invalid",
|
||||
"data": [
|
||||
1.0
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with 0 does not match false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
0
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "false is invalid",
|
||||
"data": false,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "integer zero is valid",
|
||||
"data": 0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "float zero is valid",
|
||||
"data": 0.0,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with [0] does not match [false]",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
[
|
||||
0
|
||||
]
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "[false] is invalid",
|
||||
"data": [
|
||||
false
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "[0] is valid",
|
||||
"data": [
|
||||
0
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "[0.0] is valid",
|
||||
"data": [
|
||||
0.0
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with 1 does not match true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
1
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "true is invalid",
|
||||
"data": true,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "integer one is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "float one is valid",
|
||||
"data": 1.0,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum with [1] does not match [true]",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
[
|
||||
1
|
||||
]
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "[true] is invalid",
|
||||
"data": [
|
||||
true
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "[1] is valid",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "[1.0] is valid",
|
||||
"data": [
|
||||
1.0
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "nul characters in strings",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"enum": [
|
||||
"hello\u0000there"
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "match string with nul",
|
||||
"data": "hello\u0000there",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "do not match string lacking nul",
|
||||
"data": "hellothere",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "enum schema validation",
|
||||
"enums": [
|
||||
{
|
||||
"name": "task_priority",
|
||||
"values": [
|
||||
"low",
|
||||
"medium",
|
||||
"high",
|
||||
"urgent"
|
||||
],
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "task_priority",
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"low",
|
||||
"medium",
|
||||
"high",
|
||||
"urgent"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"types": [],
|
||||
"puncs": [
|
||||
{
|
||||
"name": "enum_test_punc",
|
||||
"public": false,
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "enum_test_punc.request",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"priority": {
|
||||
"$ref": "task_priority"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"priority"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid enum value",
|
||||
"schema_id": "enum_test_punc.request",
|
||||
"data": {
|
||||
"priority": "high"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid enum value",
|
||||
"schema_id": "enum_test_punc.request",
|
||||
"data": {
|
||||
"priority": "critical"
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "ENUM_VIOLATED",
|
||||
"path": "/priority",
|
||||
"context": "critical"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "missing required enum field",
|
||||
"schema_id": "enum_test_punc.request",
|
||||
"data": {},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "REQUIRED_FIELD_MISSING",
|
||||
"path": "/priority"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
62
tests/fixtures/old/errors.json
vendored
Normal file
62
tests/fixtures/old/errors.json
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
[
|
||||
{
|
||||
"description": "detailed error reporting",
|
||||
"enums": [],
|
||||
"types": [],
|
||||
"puncs": [
|
||||
{
|
||||
"name": "detailed_errors_test",
|
||||
"public": false,
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "detailed_errors_test.request",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"address": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"street": {
|
||||
"type": "string"
|
||||
},
|
||||
"city": {
|
||||
"type": "string",
|
||||
"maxLength": 10
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"street",
|
||||
"city"
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"address"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"description": "multiple errors in nested object",
|
||||
"data": {
|
||||
"address": {
|
||||
"street": 123,
|
||||
"city": "Supercalifragilisticexpialidocious"
|
||||
}
|
||||
},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "TYPE_MISMATCH",
|
||||
"path": "/address/street"
|
||||
},
|
||||
{
|
||||
"code": "MAX_LENGTH_VIOLATED",
|
||||
"path": "/address/city"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
26
tests/fixtures/old/exclusiveMaximum.json
vendored
Normal file
26
tests/fixtures/old/exclusiveMaximum.json
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
[
|
||||
{
|
||||
"description": "exclusiveMaximum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"exclusiveMaximum": 3.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "below the exclusiveMaximum is valid",
|
||||
"data": 2.2,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point is invalid",
|
||||
"data": 3.0,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "above the exclusiveMaximum is invalid",
|
||||
"data": 3.5,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
26
tests/fixtures/old/exclusiveMinimum.json
vendored
Normal file
26
tests/fixtures/old/exclusiveMinimum.json
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
[
|
||||
{
|
||||
"description": "exclusiveMinimum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"exclusiveMinimum": 1.1
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "above the exclusiveMinimum is valid",
|
||||
"data": 1.2,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point is invalid",
|
||||
"data": 1.1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "below the exclusiveMinimum is invalid",
|
||||
"data": 0.6,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
307
tests/fixtures/old/extensible.json
vendored
Normal file
307
tests/fixtures/old/extensible.json
vendored
Normal file
@ -0,0 +1,307 @@
|
||||
[
|
||||
{
|
||||
"description": "[content.json] validation of string-encoded content based on media type",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contentMediaType": "application/json"
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[content.json] validation of binary string-encoding",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contentEncoding": "base64"
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[content.json] validation of binary-encoded media type documents",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contentMediaType": "application/json",
|
||||
"contentEncoding": "base64"
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[content.json] validation of binary-encoded media type documents with schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contentMediaType": "application/json",
|
||||
"contentEncoding": "base64",
|
||||
"contentSchema": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"foo"
|
||||
],
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[uniqueItems.json] uniqueItems with an array of items",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"prefixItems": [
|
||||
{
|
||||
"type": "boolean"
|
||||
},
|
||||
{
|
||||
"type": "boolean"
|
||||
}
|
||||
],
|
||||
"uniqueItems": true
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[uniqueItems.json] uniqueItems=false with an array of items",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"prefixItems": [
|
||||
{
|
||||
"type": "boolean"
|
||||
},
|
||||
{
|
||||
"type": "boolean"
|
||||
}
|
||||
],
|
||||
"uniqueItems": false
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[minItems.json] minItems validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minItems": 1
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[exclusiveMinimum.json] exclusiveMinimum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"exclusiveMinimum": 1.1
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[const.json] const with array",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"const": [
|
||||
{
|
||||
"foo": "bar"
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[punc.json] punc-specific resolution and local refs",
|
||||
"schema": null,
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[propertyNames.json] propertyNames validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"propertyNames": {
|
||||
"maxLength": 3
|
||||
}
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[not.json] collect annotations inside a 'not', even if collection is disabled",
|
||||
"schema": null,
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[items.json] non-array instances are valid",
|
||||
"schema": null,
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[items.json] Evaluated items collection needs to consider instance location",
|
||||
"schema": null,
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[minProperties.json] minProperties validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minProperties": 1
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[properties.json] properties whose names are Javascript object property names",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"__proto__": {
|
||||
"type": "number"
|
||||
},
|
||||
"toString": {
|
||||
"properties": {
|
||||
"length": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"constructor": {
|
||||
"type": "number"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[maxLength.json] maxLength validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxLength": 2
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[dependentSchemas.json] single dependency",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"dependentSchemas": {
|
||||
"bar": {
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "integer"
|
||||
},
|
||||
"bar": {
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[dependentSchemas.json] dependent subschema incompatible with root",
|
||||
"schema": null,
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[exclusiveMaximum.json] exclusiveMaximum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"exclusiveMaximum": 3.0
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[minimum.json] minimum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minimum": 1.1
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[minimum.json] minimum validation with signed integer",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minimum": -2
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[pattern.json] pattern validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"pattern": "^a*$"
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[maxProperties.json] maxProperties validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxProperties": 2
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[dependentRequired.json] single dependency",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"dependentRequired": {
|
||||
"bar": [
|
||||
"foo"
|
||||
]
|
||||
}
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[required.json] required properties whose names are Javascript object property names",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"required": [
|
||||
"__proto__",
|
||||
"toString",
|
||||
"constructor"
|
||||
]
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[multipleOf.json] by int",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"multipleOf": 2
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[patternProperties.json] patternProperties validates properties matching a regex",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"patternProperties": {
|
||||
"f.*o": {
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[maximum.json] maximum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maximum": 3.0
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[minLength.json] minLength validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minLength": 2
|
||||
},
|
||||
"tests": []
|
||||
},
|
||||
{
|
||||
"description": "[maxItems.json] maxItems validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxItems": 2
|
||||
},
|
||||
"tests": []
|
||||
}
|
||||
]
|
||||
1
tests/fixtures/old/format.json
vendored
Normal file
1
tests/fixtures/old/format.json
vendored
Normal file
@ -0,0 +1 @@
|
||||
[]
|
||||
324
tests/fixtures/old/if-then-else.json
vendored
Normal file
324
tests/fixtures/old/if-then-else.json
vendored
Normal file
@ -0,0 +1,324 @@
|
||||
[
|
||||
{
|
||||
"description": "ignore if without then or else",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"if": {
|
||||
"const": 0
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid when valid against lone if",
|
||||
"data": 0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "valid when invalid against lone if",
|
||||
"data": "hello",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "ignore then without if",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"then": {
|
||||
"const": 0
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid when valid against lone then",
|
||||
"data": 0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "valid when invalid against lone then",
|
||||
"data": "hello",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "ignore else without if",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"else": {
|
||||
"const": 0
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid when valid against lone else",
|
||||
"data": 0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "valid when invalid against lone else",
|
||||
"data": "hello",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "if and then without else",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"if": {
|
||||
"exclusiveMaximum": 0
|
||||
},
|
||||
"then": {
|
||||
"minimum": -10
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid through then",
|
||||
"data": -1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid through then",
|
||||
"data": -100,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "valid when if test fails",
|
||||
"data": 3,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "if and else without then",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"if": {
|
||||
"exclusiveMaximum": 0
|
||||
},
|
||||
"else": {
|
||||
"multipleOf": 2
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid when if test passes",
|
||||
"data": -1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "valid through else",
|
||||
"data": 4,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid through else",
|
||||
"data": 3,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "validate against correct branch, then vs else",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"if": {
|
||||
"exclusiveMaximum": 0
|
||||
},
|
||||
"then": {
|
||||
"minimum": -10
|
||||
},
|
||||
"else": {
|
||||
"multipleOf": 2
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid through then",
|
||||
"data": -1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid through then",
|
||||
"data": -100,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "valid through else",
|
||||
"data": 4,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid through else",
|
||||
"data": 3,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "non-interference across combined schemas",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"if": {
|
||||
"exclusiveMaximum": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"then": {
|
||||
"minimum": -10
|
||||
}
|
||||
},
|
||||
{
|
||||
"else": {
|
||||
"multipleOf": 2
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid, but would have been invalid through then",
|
||||
"data": -100,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "valid, but would have been invalid through else",
|
||||
"data": 3,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "if with boolean schema true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"if": true,
|
||||
"then": {
|
||||
"const": "then"
|
||||
},
|
||||
"else": {
|
||||
"const": "else"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "boolean schema true in if always chooses the then path (valid)",
|
||||
"data": "then",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boolean schema true in if always chooses the then path (invalid)",
|
||||
"data": "else",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "if with boolean schema false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"if": false,
|
||||
"then": {
|
||||
"const": "then"
|
||||
},
|
||||
"else": {
|
||||
"const": "else"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "boolean schema false in if always chooses the else path (invalid)",
|
||||
"data": "then",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "boolean schema false in if always chooses the else path (valid)",
|
||||
"data": "else",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "if appears at the end when serialized (keyword processing sequence)",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"then": {
|
||||
"const": "yes"
|
||||
},
|
||||
"else": {
|
||||
"const": "other"
|
||||
},
|
||||
"if": {
|
||||
"maxLength": 4
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "yes redirects to then and passes",
|
||||
"data": "yes",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "other redirects to else and passes",
|
||||
"data": "other",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "no redirects to then and fails",
|
||||
"data": "no",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "invalid redirects to else and fails",
|
||||
"data": "invalid",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "then: false fails when condition matches",
|
||||
"schema": {
|
||||
"if": {
|
||||
"const": 1
|
||||
},
|
||||
"then": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "matches if \u2192 then=false \u2192 invalid",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "does not match if \u2192 then ignored \u2192 valid",
|
||||
"data": 2,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "else: false fails when condition does not match",
|
||||
"schema": {
|
||||
"if": {
|
||||
"const": 1
|
||||
},
|
||||
"else": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "matches if \u2192 else ignored \u2192 valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "does not match if \u2192 else executes \u2192 invalid",
|
||||
"data": 2,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
81
tests/fixtures/old/infinite-loop-detection.json
vendored
Executable file
81
tests/fixtures/old/infinite-loop-detection.json
vendored
Executable file
@ -0,0 +1,81 @@
|
||||
[
|
||||
{
|
||||
"description": "evaluating the same schema location against the same data location twice is not a sign of an infinite loop",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$defs": {
|
||||
"int": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"properties": {
|
||||
"foo": {
|
||||
"$ref": "#/$defs/int"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"additionalProperties": {
|
||||
"$ref": "#/$defs/int"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "passing case",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "failing case",
|
||||
"data": {
|
||||
"foo": "a string"
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "guard against infinite recursion",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$defs": {
|
||||
"alice": {
|
||||
"$anchor": "alice",
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#bob"
|
||||
}
|
||||
]
|
||||
},
|
||||
"bob": {
|
||||
"$anchor": "bob",
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#alice"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"$ref": "#alice"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "infinite recursion detected",
|
||||
"data": {},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "MAX_DEPTH_REACHED",
|
||||
"path": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
318
tests/fixtures/old/items.json
vendored
Normal file
318
tests/fixtures/old/items.json
vendored
Normal file
@ -0,0 +1,318 @@
|
||||
[
|
||||
{
|
||||
"description": "items with boolean schema (false)",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"items": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any non-empty array is invalid",
|
||||
"data": [
|
||||
1,
|
||||
"foo",
|
||||
true
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty array is valid",
|
||||
"data": [],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "items and subitems",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$defs": {
|
||||
"item": {
|
||||
"type": "array",
|
||||
"items": false,
|
||||
"prefixItems": [
|
||||
{
|
||||
"$ref": "#/$defs/sub-item"
|
||||
},
|
||||
{
|
||||
"$ref": "#/$defs/sub-item"
|
||||
}
|
||||
]
|
||||
},
|
||||
"sub-item": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"foo"
|
||||
]
|
||||
}
|
||||
},
|
||||
"type": "array",
|
||||
"items": false,
|
||||
"prefixItems": [
|
||||
{
|
||||
"$ref": "#/$defs/item"
|
||||
},
|
||||
{
|
||||
"$ref": "#/$defs/item"
|
||||
},
|
||||
{
|
||||
"$ref": "#/$defs/item"
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid items",
|
||||
"data": [
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
]
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too many items",
|
||||
"data": [
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
]
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "too many sub-items",
|
||||
"data": [
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
]
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "wrong item",
|
||||
"data": [
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
]
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "wrong sub-item",
|
||||
"data": [
|
||||
[
|
||||
{},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
},
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
]
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "fewer items is valid",
|
||||
"data": [
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"foo": null
|
||||
}
|
||||
]
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "items does not look in applicators, valid case",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"allOf": [
|
||||
{
|
||||
"prefixItems": [
|
||||
{
|
||||
"minimum": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"items": {
|
||||
"minimum": 5
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "prefixItems in allOf does not constrain items, invalid case",
|
||||
"data": [
|
||||
3,
|
||||
5
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "prefixItems in allOf does not constrain items, valid case",
|
||||
"data": [
|
||||
5,
|
||||
5
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "items with heterogeneous array",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"prefixItems": [
|
||||
{}
|
||||
],
|
||||
"items": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "heterogeneous invalid instance",
|
||||
"data": [
|
||||
"foo",
|
||||
"bar",
|
||||
37
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "valid instance",
|
||||
"data": [
|
||||
null
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "items with null instance elements",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"items": {
|
||||
"type": "null"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "allows null elements",
|
||||
"data": [
|
||||
null
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
102
tests/fixtures/old/maxContains.json
vendored
Normal file
102
tests/fixtures/old/maxContains.json
vendored
Normal file
@ -0,0 +1,102 @@
|
||||
[
|
||||
{
|
||||
"description": "maxContains without contains is ignored",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxContains": 1
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "one item valid against lone maxContains",
|
||||
"data": [ 1 ],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "two items still valid against lone maxContains",
|
||||
"data": [ 1, 2 ],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxContains with contains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {"const": 1},
|
||||
"maxContains": 1
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty data",
|
||||
"data": [ ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "all elements match, valid maxContains",
|
||||
"data": [ 1 ],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "all elements match, invalid maxContains",
|
||||
"data": [ 1, 1 ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "some elements match, valid maxContains",
|
||||
"data": [ 1, 2 ],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "some elements match, invalid maxContains",
|
||||
"data": [ 1, 2, 1 ],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxContains with contains, value with a decimal",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {"const": 1},
|
||||
"maxContains": 1.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "one element matches, valid maxContains",
|
||||
"data": [ 1 ],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too many elements match, invalid maxContains",
|
||||
"data": [ 1, 1 ],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minContains < maxContains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {"const": 1},
|
||||
"minContains": 1,
|
||||
"maxContains": 3
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "actual < minContains < maxContains",
|
||||
"data": [ ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "minContains < actual < maxContains",
|
||||
"data": [ 1, 1 ],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "minContains < maxContains < actual",
|
||||
"data": [ 1, 1, 1, 1 ],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
60
tests/fixtures/old/maxItems.json
vendored
Normal file
60
tests/fixtures/old/maxItems.json
vendored
Normal file
@ -0,0 +1,60 @@
|
||||
[
|
||||
{
|
||||
"description": "maxItems validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxItems": 2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "shorter is valid",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "exact length is valid",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too long is invalid",
|
||||
"data": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxItems validation with a decimal",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxItems": 2.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "shorter is valid",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too long is invalid",
|
||||
"data": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
50
tests/fixtures/old/maxLength.json
vendored
Normal file
50
tests/fixtures/old/maxLength.json
vendored
Normal file
@ -0,0 +1,50 @@
|
||||
[
|
||||
{
|
||||
"description": "maxLength validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxLength": 2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "shorter is valid",
|
||||
"data": "f",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "exact length is valid",
|
||||
"data": "fo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too long is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "two graphemes is long enough",
|
||||
"data": "\ud83d\udca9\ud83d\udca9",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxLength validation with a decimal",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxLength": 2.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "shorter is valid",
|
||||
"data": "f",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too long is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
64
tests/fixtures/old/maxProperties.json
vendored
Normal file
64
tests/fixtures/old/maxProperties.json
vendored
Normal file
@ -0,0 +1,64 @@
|
||||
[
|
||||
{
|
||||
"description": "maxProperties validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxProperties": 2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "shorter is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "exact length is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too long is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxProperties validation with a decimal",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxProperties": 2.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "shorter is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too long is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxProperties = 0 means the object is empty",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maxProperties": 0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "no properties is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "one property is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
55
tests/fixtures/old/maximum.json
vendored
Normal file
55
tests/fixtures/old/maximum.json
vendored
Normal file
@ -0,0 +1,55 @@
|
||||
[
|
||||
{
|
||||
"description": "maximum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maximum": 3.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "below the maximum is valid",
|
||||
"data": 2.6,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point is valid",
|
||||
"data": 3.0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "above the maximum is invalid",
|
||||
"data": 3.5,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maximum validation with unsigned integer",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"maximum": 300
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "below the maximum is invalid",
|
||||
"data": 299.97,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point integer is valid",
|
||||
"data": 300,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point float is valid",
|
||||
"data": 300.0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "above the maximum is invalid",
|
||||
"data": 300.5,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
224
tests/fixtures/old/minContains.json
vendored
Normal file
224
tests/fixtures/old/minContains.json
vendored
Normal file
@ -0,0 +1,224 @@
|
||||
[
|
||||
{
|
||||
"description": "minContains without contains is ignored",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minContains": 1
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "one item valid against lone minContains",
|
||||
"data": [ 1 ],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "zero items still valid against lone minContains",
|
||||
"data": [],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minContains=1 with contains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {"const": 1},
|
||||
"minContains": 1
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty data",
|
||||
"data": [ ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "no elements match",
|
||||
"data": [ 2 ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "single element matches, valid minContains",
|
||||
"data": [ 1 ],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "some elements match, valid minContains",
|
||||
"data": [ 1, 2 ],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "all elements match, valid minContains",
|
||||
"data": [ 1, 1 ],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minContains=2 with contains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {"const": 1},
|
||||
"minContains": 2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty data",
|
||||
"data": [ ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "all elements match, invalid minContains",
|
||||
"data": [ 1 ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "some elements match, invalid minContains",
|
||||
"data": [ 1, 2 ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "all elements match, valid minContains (exactly as needed)",
|
||||
"data": [ 1, 1 ],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "all elements match, valid minContains (more than needed)",
|
||||
"data": [ 1, 1, 1 ],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "some elements match, valid minContains",
|
||||
"data": [ 1, 2, 1 ],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minContains=2 with contains with a decimal value",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {"const": 1},
|
||||
"minContains": 2.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "one element matches, invalid minContains",
|
||||
"data": [ 1 ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "both elements match, valid minContains",
|
||||
"data": [ 1, 1 ],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxContains = minContains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {"const": 1},
|
||||
"maxContains": 2,
|
||||
"minContains": 2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty data",
|
||||
"data": [ ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "all elements match, invalid minContains",
|
||||
"data": [ 1 ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "all elements match, invalid maxContains",
|
||||
"data": [ 1, 1, 1 ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "all elements match, valid maxContains and minContains",
|
||||
"data": [ 1, 1 ],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "maxContains < minContains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {"const": 1},
|
||||
"maxContains": 1,
|
||||
"minContains": 3
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty data",
|
||||
"data": [ ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "invalid minContains",
|
||||
"data": [ 1 ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "invalid maxContains",
|
||||
"data": [ 1, 1, 1 ],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "invalid maxContains and minContains",
|
||||
"data": [ 1, 1 ],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minContains = 0",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {"const": 1},
|
||||
"minContains": 0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty data",
|
||||
"data": [ ],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "minContains = 0 makes contains always pass",
|
||||
"data": [ 2 ],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minContains = 0 with maxContains",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"contains": {"const": 1},
|
||||
"minContains": 0,
|
||||
"maxContains": 1
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "empty data",
|
||||
"data": [ ],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "not more than maxContains",
|
||||
"data": [ 1 ],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too many",
|
||||
"data": [ 1, 1 ],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
53
tests/fixtures/old/minItems.json
vendored
Normal file
53
tests/fixtures/old/minItems.json
vendored
Normal file
@ -0,0 +1,53 @@
|
||||
[
|
||||
{
|
||||
"description": "minItems validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minItems": 1
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "longer is valid",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "exact length is valid",
|
||||
"data": [
|
||||
1
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too short is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minItems validation with a decimal",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minItems": 1.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "longer is valid",
|
||||
"data": [
|
||||
1,
|
||||
2
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too short is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
50
tests/fixtures/old/minLength.json
vendored
Normal file
50
tests/fixtures/old/minLength.json
vendored
Normal file
@ -0,0 +1,50 @@
|
||||
[
|
||||
{
|
||||
"description": "minLength validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minLength": 2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "longer is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "exact length is valid",
|
||||
"data": "fo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too short is invalid",
|
||||
"data": "f",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "one grapheme is not long enough",
|
||||
"data": "\ud83d\udca9",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minLength validation with a decimal",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minLength": 2.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "longer is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too short is invalid",
|
||||
"data": "f",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
45
tests/fixtures/old/minProperties.json
vendored
Normal file
45
tests/fixtures/old/minProperties.json
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
[
|
||||
{
|
||||
"description": "minProperties validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minProperties": 1
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "longer is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "exact length is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too short is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minProperties validation with a decimal",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minProperties": 1.0
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "longer is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "too short is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
65
tests/fixtures/old/minimum.json
vendored
Normal file
65
tests/fixtures/old/minimum.json
vendored
Normal file
@ -0,0 +1,65 @@
|
||||
[
|
||||
{
|
||||
"description": "minimum validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minimum": 1.1
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "above the minimum is valid",
|
||||
"data": 2.6,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point is valid",
|
||||
"data": 1.1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "below the minimum is invalid",
|
||||
"data": 0.6,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "minimum validation with signed integer",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"minimum": -2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "negative above the minimum is valid",
|
||||
"data": -1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "positive above the minimum is valid",
|
||||
"data": 0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point is valid",
|
||||
"data": -2,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boundary point with float is valid",
|
||||
"data": -2.0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "float below the minimum is invalid",
|
||||
"data": -2.0001,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "int below the minimum is invalid",
|
||||
"data": -3,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
94
tests/fixtures/old/multipleOf.json
vendored
Normal file
94
tests/fixtures/old/multipleOf.json
vendored
Normal file
@ -0,0 +1,94 @@
|
||||
[
|
||||
{
|
||||
"description": "by int",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"multipleOf": 2
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "int by int",
|
||||
"data": 10,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "int by int fail",
|
||||
"data": 7,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "by number",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"multipleOf": 1.5
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "zero is multiple of anything",
|
||||
"data": 0,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "4.5 is multiple of 1.5",
|
||||
"data": 4.5,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "35 is not multiple of 1.5",
|
||||
"data": 35,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "by small number",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"multipleOf": 0.0001
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "0.0075 is multiple of 0.0001",
|
||||
"data": 0.0075,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "0.00751 is not multiple of 0.0001",
|
||||
"data": 0.00751,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "float division = inf",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "integer",
|
||||
"multipleOf": 0.123456789
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "always invalid, but naive implementations may raise an overflow error",
|
||||
"data": 1e+308,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "small multiple of large integer",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "integer",
|
||||
"multipleOf": 1e-08
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any integer is a multiple of 1e-8",
|
||||
"data": 12391239123,
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
302
tests/fixtures/old/not.json
vendored
Normal file
302
tests/fixtures/old/not.json
vendored
Normal file
@ -0,0 +1,302 @@
|
||||
[
|
||||
{
|
||||
"description": "not",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "allowed",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "disallowed",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "not multiple types",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": {
|
||||
"type": [
|
||||
"integer",
|
||||
"boolean"
|
||||
]
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "mismatch",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "other mismatch",
|
||||
"data": true,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "not more complex schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "match",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "other match",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "mismatch",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "forbidden property",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"foo": {
|
||||
"not": {}
|
||||
},
|
||||
"baz": {
|
||||
"type": "integer"
|
||||
},
|
||||
"bar": {
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "property present",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "property absent",
|
||||
"data": {
|
||||
"bar": 1,
|
||||
"baz": 2
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "forbid everything with empty schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": {}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "number is invalid",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "string is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "boolean true is invalid",
|
||||
"data": true,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "boolean false is invalid",
|
||||
"data": false,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "null is invalid",
|
||||
"data": null,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "object is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty object is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "array is invalid",
|
||||
"data": [
|
||||
"foo"
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "forbid everything with boolean schema true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "number is invalid",
|
||||
"data": 1,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "string is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "boolean true is invalid",
|
||||
"data": true,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "boolean false is invalid",
|
||||
"data": false,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "null is invalid",
|
||||
"data": null,
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "object is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty object is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "array is invalid",
|
||||
"data": [
|
||||
"foo"
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty array is invalid",
|
||||
"data": [],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "allow everything with boolean schema false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "number is valid",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "string is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boolean true is valid",
|
||||
"data": true,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "boolean false is valid",
|
||||
"data": false,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "null is valid",
|
||||
"data": null,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "object is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty object is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "array is valid",
|
||||
"data": [
|
||||
"foo"
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty array is valid",
|
||||
"data": [],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "double negation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"not": {
|
||||
"not": {}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
296
tests/fixtures/old/oneOf.json
vendored
Normal file
296
tests/fixtures/old/oneOf.json
vendored
Normal file
@ -0,0 +1,296 @@
|
||||
[
|
||||
{
|
||||
"description": "oneOf with boolean schemas, all true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"oneOf": [
|
||||
true,
|
||||
true,
|
||||
true
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "oneOf with boolean schemas, one true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"oneOf": [
|
||||
true,
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "oneOf with boolean schemas, more than one true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"oneOf": [
|
||||
true,
|
||||
true,
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "oneOf with boolean schemas, all false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"oneOf": [
|
||||
false,
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "any value is invalid",
|
||||
"data": "foo",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "oneOf complex types",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"oneOf": [
|
||||
{
|
||||
"properties": {
|
||||
"bar": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"bar"
|
||||
]
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"foo"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "first oneOf valid (complex)",
|
||||
"data": {
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "second oneOf valid (complex)",
|
||||
"data": {
|
||||
"foo": "baz"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "both oneOf valid (complex)",
|
||||
"data": {
|
||||
"foo": "baz",
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "neither oneOf valid (complex)",
|
||||
"data": {
|
||||
"foo": 2,
|
||||
"bar": "quux"
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "oneOf with empty schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "number"
|
||||
},
|
||||
{}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "one valid - valid",
|
||||
"data": "foo",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "both valid - invalid",
|
||||
"data": 123,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "oneOf with required",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "object",
|
||||
"oneOf": [
|
||||
{
|
||||
"required": [
|
||||
"foo",
|
||||
"bar"
|
||||
]
|
||||
},
|
||||
{
|
||||
"required": [
|
||||
"foo",
|
||||
"baz"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "both invalid - invalid",
|
||||
"data": {
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "first valid - valid",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "second valid - valid",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"baz": 3
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "both valid - invalid",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2,
|
||||
"baz": 3
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "oneOf with missing optional property",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"oneOf": [
|
||||
{
|
||||
"properties": {
|
||||
"bar": true,
|
||||
"baz": true
|
||||
},
|
||||
"required": [
|
||||
"bar"
|
||||
]
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"foo": true
|
||||
},
|
||||
"required": [
|
||||
"foo"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "first oneOf valid",
|
||||
"data": {
|
||||
"bar": 8
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "second oneOf valid",
|
||||
"data": {
|
||||
"foo": "foo"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "both oneOf valid",
|
||||
"data": {
|
||||
"foo": "foo",
|
||||
"bar": 8
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "neither oneOf valid",
|
||||
"data": {
|
||||
"baz": "quux"
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "nested oneOf, to check validation semantics",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"oneOf": [
|
||||
{
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "null is valid",
|
||||
"data": null,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "anything non-null is invalid",
|
||||
"data": 123,
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
128
tests/fixtures/old/override.json
vendored
Normal file
128
tests/fixtures/old/override.json
vendored
Normal file
@ -0,0 +1,128 @@
|
||||
[
|
||||
{
|
||||
"description": "override: true replaces base property definition",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$defs": {
|
||||
"base": {
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string",
|
||||
"minLength": 5
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"$ref": "#/$defs/base",
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "integer",
|
||||
"override": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid integer (overrides string type)",
|
||||
"data": {
|
||||
"foo": 123
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid integer (overrides string type)",
|
||||
"data": {
|
||||
"foo": "abc"
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "override: true in nested schema",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$defs": {
|
||||
"base": {
|
||||
"properties": {
|
||||
"config": {
|
||||
"properties": {
|
||||
"mode": {
|
||||
"const": "auto"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"$ref": "#/$defs/base",
|
||||
"properties": {
|
||||
"config": {
|
||||
"properties": {
|
||||
"mode": {
|
||||
"const": "manual",
|
||||
"override": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "matches overridden const",
|
||||
"data": {
|
||||
"config": {
|
||||
"mode": "manual"
|
||||
}
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "does not match base const",
|
||||
"data": {
|
||||
"config": {
|
||||
"mode": "auto"
|
||||
}
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "override: false (default) behavior (intersection)",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$defs": {
|
||||
"base": {
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"$ref": "#/$defs/base",
|
||||
"properties": {
|
||||
"foo": {
|
||||
"minLength": 2
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid intersection",
|
||||
"data": {
|
||||
"foo": "ok"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid intersection (wrong type)",
|
||||
"data": {
|
||||
"foo": 123
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
35
tests/fixtures/old/pattern.json
vendored
Normal file
35
tests/fixtures/old/pattern.json
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
[
|
||||
{
|
||||
"description": "pattern validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"pattern": "^a*$"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "a matching pattern is valid",
|
||||
"data": "aaa",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "a non-matching pattern is invalid",
|
||||
"data": "abc",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "pattern is not anchored",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"pattern": "a+"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "matches a substring",
|
||||
"data": "xxaayy",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
216
tests/fixtures/old/patternProperties.json
vendored
Normal file
216
tests/fixtures/old/patternProperties.json
vendored
Normal file
@ -0,0 +1,216 @@
|
||||
[
|
||||
{
|
||||
"description": "patternProperties validates properties matching a regex",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"patternProperties": {
|
||||
"f.*o": {
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "a single valid match is valid",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "multiple valid matches is valid",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"foooooo": 2
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "a single invalid match is invalid",
|
||||
"data": {
|
||||
"foo": "bar",
|
||||
"fooooo": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "multiple invalid matches is invalid",
|
||||
"data": {
|
||||
"foo": "bar",
|
||||
"foooooo": "baz"
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "multiple simultaneous patternProperties are validated",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"patternProperties": {
|
||||
"a*": {
|
||||
"type": "integer"
|
||||
},
|
||||
"aaa*": {
|
||||
"maximum": 20
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "a single valid match is valid",
|
||||
"data": {
|
||||
"a": 21
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "a simultaneous match is valid",
|
||||
"data": {
|
||||
"aaaa": 18
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "multiple matches is valid",
|
||||
"data": {
|
||||
"a": 21,
|
||||
"aaaa": 18
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "an invalid due to one is invalid",
|
||||
"data": {
|
||||
"a": "bar"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "an invalid due to the other is invalid",
|
||||
"data": {
|
||||
"aaaa": 31
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "an invalid due to both is invalid",
|
||||
"data": {
|
||||
"aaa": "foo",
|
||||
"aaaa": 31
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "regexes are not anchored by default and are case sensitive",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"patternProperties": {
|
||||
"[0-9]{2,}": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"X_": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "non recognized members are ignored",
|
||||
"data": {
|
||||
"answer 1": "42"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "recognized members are accounted for",
|
||||
"data": {
|
||||
"a31b": null
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "regexes are case sensitive",
|
||||
"data": {
|
||||
"a_x_3": 3
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "regexes are case sensitive, 2",
|
||||
"data": {
|
||||
"a_X_3": 3
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "patternProperties with boolean schemas",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"patternProperties": {
|
||||
"f.*": true,
|
||||
"b.*": false
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "object with property matching schema true is valid",
|
||||
"data": {
|
||||
"foo": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "object with property matching schema false is invalid",
|
||||
"data": {
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "object with both properties is invalid",
|
||||
"data": {
|
||||
"foo": 1,
|
||||
"bar": 2
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "object with a property matching both true and false is invalid",
|
||||
"data": {
|
||||
"foobar": 1
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty object is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "patternProperties with null valued instance properties",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"patternProperties": {
|
||||
"^.*bar$": {
|
||||
"type": "null"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "allows null values",
|
||||
"data": {
|
||||
"foobar": null
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
22
tests/fixtures/old/prefixItems.json
vendored
Normal file
22
tests/fixtures/old/prefixItems.json
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
[
|
||||
{
|
||||
"description": "prefixItems with null instance elements",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"prefixItems": [
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "allows null elements",
|
||||
"data": [
|
||||
null
|
||||
],
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
245
tests/fixtures/old/properties.json
vendored
Executable file
245
tests/fixtures/old/properties.json
vendored
Executable file
@ -0,0 +1,245 @@
|
||||
[
|
||||
{
|
||||
"description": "properties with escaped characters",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"foo\nbar": {
|
||||
"type": "number"
|
||||
},
|
||||
"foo\"bar": {
|
||||
"type": "number"
|
||||
},
|
||||
"foo\\bar": {
|
||||
"type": "number"
|
||||
},
|
||||
"foo\rbar": {
|
||||
"type": "number"
|
||||
},
|
||||
"foo\tbar": {
|
||||
"type": "number"
|
||||
},
|
||||
"foo\fbar": {
|
||||
"type": "number"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "object with all numbers is valid",
|
||||
"data": {
|
||||
"foo\nbar": 1,
|
||||
"foo\"bar": 1,
|
||||
"foo\\bar": 1,
|
||||
"foo\rbar": 1,
|
||||
"foo\tbar": 1,
|
||||
"foo\fbar": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "object with strings is invalid",
|
||||
"data": {
|
||||
"foo\nbar": "1",
|
||||
"foo\"bar": "1",
|
||||
"foo\\bar": "1",
|
||||
"foo\rbar": "1",
|
||||
"foo\tbar": "1",
|
||||
"foo\fbar": "1"
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "properties with null valued instance properties",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"foo": {
|
||||
"type": "null"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "allows null values",
|
||||
"data": {
|
||||
"foo": null
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "properties whose names are Javascript object property names",
|
||||
"comment": "Ensure JS implementations don't universally consider e.g. __proto__ to always be present in an object.",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"__proto__": {
|
||||
"type": "number"
|
||||
},
|
||||
"toString": {
|
||||
"properties": {
|
||||
"length": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"constructor": {
|
||||
"type": "number"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "none of the properties mentioned",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "__proto__ not valid",
|
||||
"data": {
|
||||
"__proto__": "foo"
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "toString not valid",
|
||||
"data": {
|
||||
"toString": {
|
||||
"length": 37
|
||||
}
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "constructor not valid",
|
||||
"data": {
|
||||
"constructor": {
|
||||
"length": 37
|
||||
}
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "all present and valid",
|
||||
"data": {
|
||||
"__proto__": 12,
|
||||
"toString": {
|
||||
"length": "foo"
|
||||
},
|
||||
"constructor": 37
|
||||
},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "property merging across multi-level inheritance",
|
||||
"enums": [],
|
||||
"puncs": [],
|
||||
"types": [
|
||||
{
|
||||
"name": "properties_test.entity",
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "properties_test.entity",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": {
|
||||
"type": "string",
|
||||
"const": "properties_test.entity"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"id"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "properties_test.user",
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "properties_test.user",
|
||||
"$ref": "properties_test.entity",
|
||||
"properties": {
|
||||
"type": {
|
||||
"type": "string",
|
||||
"const": "properties_test.user",
|
||||
"override": true
|
||||
},
|
||||
"password": {
|
||||
"type": "string",
|
||||
"minLength": 8
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"password"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "properties_test.person",
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "properties_test.person",
|
||||
"$ref": "properties_test.user",
|
||||
"properties": {
|
||||
"type": {
|
||||
"type": "string",
|
||||
"const": "properties_test.person",
|
||||
"override": true
|
||||
},
|
||||
"first_name": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"last_name": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"first_name",
|
||||
"last_name"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"description": "person inherits all properties correctly",
|
||||
"schema_id": "properties_test.person",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "validation keywords inherited and applied",
|
||||
"schema_id": "properties_test.person",
|
||||
"data": {},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "MIN_LENGTH_VIOLATED",
|
||||
"path": "/password"
|
||||
},
|
||||
{
|
||||
"code": "MIN_LENGTH_VIOLATED",
|
||||
"path": "/first_name"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
152
tests/fixtures/old/propertyNames.json
vendored
Normal file
152
tests/fixtures/old/propertyNames.json
vendored
Normal file
@ -0,0 +1,152 @@
|
||||
[
|
||||
{
|
||||
"description": "propertyNames validation",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"propertyNames": {
|
||||
"maxLength": 3
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "all property names valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "some property names invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "object without properties is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "propertyNames validation with pattern",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"propertyNames": {
|
||||
"pattern": "^a+$"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "matching property names valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "non-matching property name is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "object without properties is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "propertyNames with boolean schema true",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"propertyNames": true
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "object with any properties is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "empty object is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "propertyNames with boolean schema false",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"propertyNames": false
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "object with any properties is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty object is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "propertyNames with const",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"propertyNames": {
|
||||
"const": "foo"
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "object with property foo is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "object with any other property is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty object is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "propertyNames with enum",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"propertyNames": {
|
||||
"enum": [
|
||||
"foo",
|
||||
"bar"
|
||||
]
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "object with property foo is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "object with property foo and bar is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "object with any other property is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "empty object is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
200
tests/fixtures/old/punc.json
vendored
Normal file
200
tests/fixtures/old/punc.json
vendored
Normal file
@ -0,0 +1,200 @@
|
||||
[
|
||||
{
|
||||
"description": "punc-specific resolution and local refs",
|
||||
"enums": [],
|
||||
"types": [
|
||||
{
|
||||
"name": "global_thing",
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "global_thing",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
},
|
||||
"type": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"id",
|
||||
"type"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "punc_entity",
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "punc_entity",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"id",
|
||||
"type"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "punc_person",
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "punc_person",
|
||||
"$ref": "punc_entity",
|
||||
"properties": {
|
||||
"first_name": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"last_name": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"address": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"street": {
|
||||
"type": "string"
|
||||
},
|
||||
"city": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"street",
|
||||
"city"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"puncs": [
|
||||
{
|
||||
"name": "public_ref_test",
|
||||
"public": true,
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "public_ref_test.request",
|
||||
"$ref": "punc_person"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "private_ref_test",
|
||||
"public": false,
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "private_ref_test.request",
|
||||
"$ref": "punc_person"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "punc_with_local_ref_test",
|
||||
"public": false,
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "local_address",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"street": {
|
||||
"type": "string"
|
||||
},
|
||||
"city": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"street",
|
||||
"city"
|
||||
]
|
||||
},
|
||||
{
|
||||
"$id": "punc_with_local_ref_test.request",
|
||||
"$ref": "local_address"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "punc_with_local_ref_to_global_test",
|
||||
"public": false,
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "local_user_with_thing",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"user_name": {
|
||||
"type": "string"
|
||||
},
|
||||
"thing": {
|
||||
"$ref": "global_thing"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"user_name",
|
||||
"thing"
|
||||
]
|
||||
},
|
||||
{
|
||||
"$id": "punc_with_local_ref_to_global_test.request",
|
||||
"$ref": "local_user_with_thing"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid instance with address passes in public punc",
|
||||
"schema_id": "public_ref_test.request",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "punc local ref resolution",
|
||||
"schema_id": "punc_with_local_ref_test.request",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "punc local ref missing requirement",
|
||||
"schema_id": "punc_with_local_ref_test.request",
|
||||
"data": {},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "REQUIRED_FIELD_MISSING",
|
||||
"path": "/city"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "punc local ref to global type - invalid format",
|
||||
"schema_id": "punc_with_local_ref_to_global_test.request",
|
||||
"data": {},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "FORMAT_INVALID",
|
||||
"path": "/thing/id"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
208
tests/fixtures/old/ref.json
vendored
Executable file
208
tests/fixtures/old/ref.json
vendored
Executable file
@ -0,0 +1,208 @@
|
||||
[
|
||||
{
|
||||
"description": "root pointer ref",
|
||||
"schema": {
|
||||
"properties": {
|
||||
"foo": {
|
||||
"$ref": "#"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "match self",
|
||||
"data": {
|
||||
"foo": false
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "recursive match",
|
||||
"data": {
|
||||
"foo": {
|
||||
"foo": false
|
||||
}
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "mismatch",
|
||||
"data": {
|
||||
"bar": false
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "recursive mismatch",
|
||||
"data": {
|
||||
"foo": {
|
||||
"bar": false
|
||||
}
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "relative pointer ref to object",
|
||||
"schema": {
|
||||
"$id": "http://example.com/root.json",
|
||||
"definitions": {
|
||||
"foo": {
|
||||
"type": "integer"
|
||||
},
|
||||
"bar": {
|
||||
"$ref": "#/definitions/foo"
|
||||
}
|
||||
},
|
||||
"$ref": "#/definitions/bar"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "match integer",
|
||||
"data": 1,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "mismatch string",
|
||||
"data": "a",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "escaped pointer ref",
|
||||
"schema": {
|
||||
"$id": "http://example.com/tilda.json",
|
||||
"definitions": {
|
||||
"tilde~field": {
|
||||
"type": "integer"
|
||||
},
|
||||
"slash/field": {
|
||||
"type": "integer"
|
||||
},
|
||||
"percent%field": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"properties": {
|
||||
"tilde": {
|
||||
"$ref": "#/definitions/tilde~0field"
|
||||
},
|
||||
"slash": {
|
||||
"$ref": "#/definitions/slash~1field"
|
||||
},
|
||||
"percent": {
|
||||
"$ref": "#/definitions/percent%25field"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "slash valid",
|
||||
"data": {
|
||||
"slash": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "tilde valid",
|
||||
"data": {
|
||||
"tilde": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "percent valid",
|
||||
"data": {
|
||||
"percent": 1
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "slash invalid",
|
||||
"data": {
|
||||
"slash": "a"
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "nested refs",
|
||||
"schema": {
|
||||
"$id": "http://example.com/nested.json",
|
||||
"definitions": {
|
||||
"a": {
|
||||
"type": "integer"
|
||||
},
|
||||
"b": {
|
||||
"$ref": "#/definitions/a"
|
||||
},
|
||||
"c": {
|
||||
"$ref": "#/definitions/b"
|
||||
}
|
||||
},
|
||||
"$ref": "#/definitions/c"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "nested match",
|
||||
"data": 5,
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "nested mismatch",
|
||||
"data": "a",
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "ref to array item",
|
||||
"schema": {
|
||||
"prefixItems": [
|
||||
{
|
||||
"type": "integer"
|
||||
},
|
||||
{
|
||||
"type": "string"
|
||||
}
|
||||
],
|
||||
"definitions": {
|
||||
"first": {
|
||||
"$ref": "#/prefixItems/0"
|
||||
},
|
||||
"second": {
|
||||
"$ref": "#/prefixItems/1"
|
||||
}
|
||||
},
|
||||
"properties": {
|
||||
"a": {
|
||||
"$ref": "#/definitions/first"
|
||||
},
|
||||
"b": {
|
||||
"$ref": "#/definitions/second"
|
||||
}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "valid array items",
|
||||
"data": {
|
||||
"a": 1,
|
||||
"b": "foo"
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "invalid array items",
|
||||
"data": {
|
||||
"a": "foo",
|
||||
"b": 1
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
279
tests/fixtures/old/required.json
vendored
Normal file
279
tests/fixtures/old/required.json
vendored
Normal file
@ -0,0 +1,279 @@
|
||||
[
|
||||
{
|
||||
"description": "required with empty array",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"properties": {
|
||||
"foo": {}
|
||||
},
|
||||
"required": []
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "property not required",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "required with escaped characters",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"required": [
|
||||
"foo\nbar",
|
||||
"foo\"bar",
|
||||
"foo\\bar",
|
||||
"foo\rbar",
|
||||
"foo\tbar",
|
||||
"foo\fbar"
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "object with all properties present is valid",
|
||||
"data": {},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "object with some properties missing is invalid",
|
||||
"data": {},
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "required properties whose names are Javascript object property names",
|
||||
"comment": "Ensure JS implementations don't universally consider e.g. __proto__ to always be present in an object.",
|
||||
"schema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"required": [
|
||||
"__proto__",
|
||||
"toString",
|
||||
"constructor"
|
||||
]
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "none of the properties mentioned",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "__proto__ present",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "toString present",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "constructor present",
|
||||
"data": {},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "all present",
|
||||
"data": {},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "basic required property validation",
|
||||
"enums": [],
|
||||
"types": [],
|
||||
"puncs": [
|
||||
{
|
||||
"name": "basic_validation_test",
|
||||
"public": false,
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "basic_validation_test.request",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"age": {
|
||||
"type": "integer",
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"name",
|
||||
"age"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"description": "missing all required fields",
|
||||
"data": {},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "REQUIRED_FIELD_MISSING",
|
||||
"path": "/name"
|
||||
},
|
||||
{
|
||||
"code": "REQUIRED_FIELD_MISSING",
|
||||
"path": "/age"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "missing some required fields",
|
||||
"data": {},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "REQUIRED_FIELD_MISSING",
|
||||
"path": "/age"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "required property merging across inheritance",
|
||||
"enums": [],
|
||||
"puncs": [],
|
||||
"types": [
|
||||
{
|
||||
"name": "entity",
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "entity",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
},
|
||||
"type": {
|
||||
"type": "string"
|
||||
},
|
||||
"created_by": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"id",
|
||||
"type",
|
||||
"created_by"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "user",
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "user",
|
||||
"base": "entity",
|
||||
"$ref": "entity",
|
||||
"properties": {
|
||||
"password": {
|
||||
"type": "string",
|
||||
"minLength": 8
|
||||
}
|
||||
},
|
||||
"if": {
|
||||
"properties": {
|
||||
"type": {
|
||||
"const": "user"
|
||||
}
|
||||
}
|
||||
},
|
||||
"then": {
|
||||
"required": [
|
||||
"password"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "person",
|
||||
"schemas": [
|
||||
{
|
||||
"$id": "person",
|
||||
"base": "user",
|
||||
"$ref": "user",
|
||||
"properties": {
|
||||
"first_name": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"last_name": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
}
|
||||
},
|
||||
"if": {
|
||||
"properties": {
|
||||
"type": {
|
||||
"const": "person"
|
||||
}
|
||||
}
|
||||
},
|
||||
"then": {
|
||||
"required": [
|
||||
"first_name",
|
||||
"last_name"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"description": "missing all required fields across inheritance chain",
|
||||
"schema_id": "person",
|
||||
"data": {},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "REQUIRED_FIELD_MISSING",
|
||||
"path": "/id"
|
||||
},
|
||||
{
|
||||
"code": "REQUIRED_FIELD_MISSING",
|
||||
"path": "/created_by"
|
||||
},
|
||||
{
|
||||
"code": "REQUIRED_FIELD_MISSING",
|
||||
"path": "/first_name"
|
||||
},
|
||||
{
|
||||
"code": "REQUIRED_FIELD_MISSING",
|
||||
"path": "/last_name"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "conditional requirements through inheritance",
|
||||
"schema_id": "person",
|
||||
"data": {},
|
||||
"valid": false,
|
||||
"expect_errors": [
|
||||
{
|
||||
"code": "REQUIRED_FIELD_MISSING",
|
||||
"path": "/first_name"
|
||||
},
|
||||
{
|
||||
"code": "REQUIRED_FIELD_MISSING",
|
||||
"path": "/last_name"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user