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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
273 changes: 186 additions & 87 deletions lib/dsc-lib-jsonschema/src/schema_utility_extensions.rs

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion lib/dsc-lib-jsonschema/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@
//! of the modules from the rest of the source tree.

#[cfg(test)] mod schema_utility_extensions;
#[cfg(test)] mod transforms;
#[cfg(test)] mod vscode;
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ static OBJECT_VALUE: LazyLock<Map<String, Value>> = LazyLock::new(|| json!({
}).as_object().unwrap().clone());
static NULL_VALUE: () = ();
static STRING_VALUE: &str = "value";
static SUBSCHEMA_VALUE: LazyLock<Schema> = LazyLock::new(|| json_schema!({
"$id": "https://schema.contoso.com/test/get_keyword_as/subschema.json"
}));
static TEST_SCHEMA: LazyLock<Schema> = LazyLock::new(|| json_schema!({
"$id": "https://schema.contoso.com/test/get_keyword_as.json",
"array": *ARRAY_VALUE,
Expand All @@ -35,6 +38,7 @@ static TEST_SCHEMA: LazyLock<Schema> = LazyLock::new(|| json_schema!({
"object": *OBJECT_VALUE,
"null": null,
"string": *STRING_VALUE,
"subschema": *SUBSCHEMA_VALUE,
}));

/// Defines test cases for a given `get_keyword_as` function (non-mutable).
Expand Down Expand Up @@ -135,12 +139,14 @@ test_cases_for_get_keyword_as!(
get_keyword_as_number: "array", "integer", Some(&(INTEGER_VALUE.into())),
get_keyword_as_str: "array", "string", Some(STRING_VALUE),
get_keyword_as_string: "array", "string", Some(STRING_VALUE.to_string()),
get_keyword_as_subschema: "array", "subschema", Some(&*SUBSCHEMA_VALUE),
);


test_cases_for_get_keyword_as_mut!(
get_keyword_as_array_mut: "boolean", "array", Some(&mut (*ARRAY_VALUE).clone()),
get_keyword_as_object_mut: "array", "object", Some(&mut (*OBJECT_VALUE).clone()),
get_keyword_as_subschema_mut: "array", "subschema", Some(&mut (*SUBSCHEMA_VALUE).clone()),
);

#[cfg(test)] mod get_id {
Expand Down Expand Up @@ -335,7 +341,6 @@ test_cases_for_get_keyword_as_mut!(

use pretty_assertions::assert_eq;
use schemars::json_schema;
use serde_json::json;

use crate::schema_utility_extensions::SchemaUtilityExtensions;

Expand Down Expand Up @@ -382,13 +387,13 @@ test_cases_for_get_keyword_as_mut!(
}
}
});
let ref expected = json!({
let ref expected = json_schema!({
"$id": "https://contoso.com/schemas/foo.json",
"title": "Foo"
});
assert_eq!(
schema.get_defs_subschema_from_id("https://contoso.com/schemas/foo.json"),
expected.as_object()
Some(expected)
);
}
}
Expand All @@ -397,7 +402,6 @@ test_cases_for_get_keyword_as_mut!(

use pretty_assertions::assert_eq;
use schemars::json_schema;
use serde_json::json;

use crate::schema_utility_extensions::SchemaUtilityExtensions;

Expand Down Expand Up @@ -444,20 +448,19 @@ test_cases_for_get_keyword_as_mut!(
}
}
});
let ref mut expected = json!({
let ref mut expected = json_schema!({
"$id": "https://contoso.com/schemas/foo.json",
"title": "Foo"
});
assert_eq!(
schema.get_defs_subschema_from_id_mut("https://contoso.com/schemas/foo.json"),
expected.as_object_mut()
Some(expected)
);
}
}
#[cfg(test)] mod get_defs_subschema_from_reference {
use pretty_assertions::assert_eq;
use schemars::json_schema;
use serde_json::json;

use crate::schema_utility_extensions::SchemaUtilityExtensions;

Expand Down Expand Up @@ -518,13 +521,13 @@ test_cases_for_get_keyword_as_mut!(
}
}
});
let expected = json!({
let ref expected = json_schema!({
"$id": "https://contoso.com/schemas/foo.json",
"title": "Foo"
});
assert_eq!(
schema.get_defs_subschema_from_reference("#/$defs/foo").unwrap(),
expected.as_object().unwrap()
expected
);
}
#[test] fn with_absolute_id_uri_reference() {
Expand All @@ -539,13 +542,13 @@ test_cases_for_get_keyword_as_mut!(
}
}
});
let expected = json!({
let ref expected = json_schema!({
"$id": "https://contoso.com/schemas/foo.json",
"title": "Foo"
});
assert_eq!(
schema.get_defs_subschema_from_reference("/schemas/foo.json").unwrap(),
expected.as_object().unwrap()
expected
);
}
#[test] fn with_relative_id_uri_reference() {
Expand All @@ -560,21 +563,20 @@ test_cases_for_get_keyword_as_mut!(
}
}
});
let expected = json!({
let ref expected = json_schema!({
"$id": "https://contoso.com/schemas/foo.json",
"title": "Foo"
});
assert_eq!(
schema.get_defs_subschema_from_reference("https://contoso.com/schemas/foo.json").unwrap(),
expected.as_object().unwrap()
expected
);
}
}

#[cfg(test)] mod get_defs_subschema_from_reference_mut {
use pretty_assertions::assert_ne;
use schemars::json_schema;
use serde_json::json;

use crate::schema_utility_extensions::SchemaUtilityExtensions;

Expand Down Expand Up @@ -642,14 +644,14 @@ test_cases_for_get_keyword_as_mut!(
}
}
});
let ref mut expected = json!({
let ref mut expected = json_schema!({
"$id": "https://contoso.com/schemas/foo.json",
"title": "Foo"
});

assert_ne!(
schema.get_defs_subschema_from_reference_mut("#/$defs/foo"),
expected.as_object_mut()
Some(expected)
);
}
#[test] fn with_absolute_id_uri_reference() {
Expand All @@ -664,13 +666,13 @@ test_cases_for_get_keyword_as_mut!(
}
}
});
let ref mut expected = json!({
let ref mut expected = json_schema!({
"$id": "https://contoso.com/schemas/foo.json",
"title": "Foo"
});
assert_eq!(
schema.get_defs_subschema_from_reference_mut("/schemas/foo.json").unwrap(),
expected.as_object_mut().unwrap()
expected
);
}
#[test] fn with_relative_id_uri_reference() {
Expand All @@ -685,13 +687,13 @@ test_cases_for_get_keyword_as_mut!(
}
}
});
let ref mut expected = json!({
let ref mut expected = json_schema!({
"$id": "https://contoso.com/schemas/foo.json",
"title": "Foo"
});
assert_eq!(
schema.get_defs_subschema_from_reference_mut("https://contoso.com/schemas/foo.json").unwrap(),
expected.as_object_mut().unwrap()
expected
);
}
}
Expand Down Expand Up @@ -891,7 +893,6 @@ test_cases_for_get_keyword_as_mut!(

use pretty_assertions::assert_eq;
use schemars::json_schema;
use serde_json::json;

use crate::schema_utility_extensions::SchemaUtilityExtensions;

Expand Down Expand Up @@ -924,7 +925,7 @@ test_cases_for_get_keyword_as_mut!(
assert_eq!(schema.get_property_subschema("foo"), None)
}
#[test] fn when_given_property_is_object() {
let ref property = json!({
let ref property = json_schema!({
"title": "Foo property"
});
let ref schema = json_schema!({
Expand All @@ -934,7 +935,7 @@ test_cases_for_get_keyword_as_mut!(
});
assert_eq!(
schema.get_property_subschema("foo").unwrap(),
property.as_object().unwrap()
property
)
}
}
Expand All @@ -944,7 +945,6 @@ test_cases_for_get_keyword_as_mut!(

use pretty_assertions::assert_eq;
use schemars::json_schema;
use serde_json::json;

use crate::schema_utility_extensions::SchemaUtilityExtensions;

Expand Down Expand Up @@ -977,7 +977,7 @@ test_cases_for_get_keyword_as_mut!(
assert_eq!(schema.get_property_subschema_mut("foo"), None)
}
#[test] fn when_given_property_is_object() {
let ref mut property = json!({
let ref mut property = json_schema!({
"title": "Foo property"
});
let ref mut schema = json_schema!({
Expand All @@ -987,7 +987,7 @@ test_cases_for_get_keyword_as_mut!(
});
assert_eq!(
schema.get_property_subschema_mut("foo").unwrap(),
property.as_object_mut().unwrap()
property
)
}
}
4 changes: 0 additions & 4 deletions lib/dsc-lib-jsonschema/src/tests/transforms/mod.rs

This file was deleted.

Loading