Skip to content

Commit

Permalink
support for additionalItems (#89)
Browse files Browse the repository at this point in the history
support for additionalItems with pre draft2020-12 semantics
  • Loading branch information
hudson-ai authored Dec 13, 2024
1 parent 011fe3b commit b951522
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion parser/src/json/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const DEFAULT_DRAFT: Draft = Draft::Draft202012;
const TYPES: [&str; 6] = ["null", "boolean", "number", "string", "array", "object"];

// Keywords that are implemented in this module
const IMPLEMENTED: [&str; 22] = [
const IMPLEMENTED: [&str; 23] = [
// Core
"anyOf",
"oneOf",
Expand All @@ -26,6 +26,7 @@ const IMPLEMENTED: [&str; 22] = [
"type",
// Array
"items",
"additionalItems",
"prefixItems",
"minItems",
"maxItems",
Expand Down Expand Up @@ -611,6 +612,7 @@ fn compile_type(ctx: &Context, tp: &str, schema: &HashMap<&str, &Value>) -> Resu
get("maxItems"),
get("prefixItems"),
get("items"),
get("additionalItems"),
),
"object" => compile_object(
ctx,
Expand Down Expand Up @@ -726,7 +728,22 @@ fn compile_array(
max_items: Option<&Value>,
prefix_items: Option<&Value>,
items: Option<&Value>,
additional_items: Option<&Value>,
) -> Result<Schema> {
let (prefix_items, items) = {
// Note that draft detection falls back to Draft202012 if the draft is unknown, so let's relax the draft constraint a bit
// and assume we're in an old draft if additionalItems is present or items is an array
if ctx.draft <= Draft::Draft201909 || additional_items.is_some() || matches!(items, Some(Value::Array(..))) {
match (items, additional_items) {
// Treat array items as prefixItems and additionalItems as items in draft 2019-09 and earlier
(Some(Value::Array(..)), _) => (items, additional_items),
// items is treated as items, and additionalItems is ignored if items is not an array (or is missing)
_ => (None, items),
}
} else {
(prefix_items, items)
}
};
let min_items = match min_items {
None => 0,
Some(val) => val
Expand Down

0 comments on commit b951522

Please sign in to comment.