]> git.proxmox.com Git - proxmox.git/commitdiff
schema: add regression tests for additional_properties in AllOf
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 21 Feb 2024 11:02:00 +0000 (12:02 +0100)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 21 Feb 2024 11:02:00 +0000 (12:02 +0100)
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
proxmox-schema/tests/schema_verification.rs

index 7df5d7a43fae32080becb424f773ee482aae243b..4eef74877a36a113db39f6d3261b695e2acd96bb 100644 (file)
@@ -37,6 +37,37 @@ static NESTED_PROPERTY_SCHEMA: Schema = ObjectSchema::new(
 )
 .schema();
 
+static ANOTHER_OBJECT_SCHEMA: Schema = ObjectSchema::new(
+    "another simple object schema",
+    &[
+        ("another1", false, &STRING_SCHEMA),
+        ("another2", true, &STRING_SCHEMA),
+    ],
+)
+.schema();
+
+static OBJECT_WITH_ADDITIONAL: Schema = ObjectSchema::new(
+    "object allowing additional properties",
+    &[
+        ("regular1", false, &STRING_SCHEMA),
+        ("regular2", true, &STRING_SCHEMA),
+    ],
+)
+.additional_properties(true)
+.schema();
+
+static ALL_OF_SCHEMA_NO_ADDITIONAL: Schema = AllOfSchema::new(
+    "flattening 2 objects together",
+    &[&SIMPLE_OBJECT_SCHEMA, &ANOTHER_OBJECT_SCHEMA],
+)
+.schema();
+
+static ALL_OF_SCHEMA_ADDITIONAL: Schema = AllOfSchema::new(
+    "flattening 2 objects together where 1 allows additional properties",
+    &[&SIMPLE_OBJECT_SCHEMA, &OBJECT_WITH_ADDITIONAL],
+)
+.schema();
+
 fn compare_error(expected: &[(&str, &str)], err: Error) -> Result<(), Error> {
     let err = match err.downcast_ref::<ParameterError>() {
         Some(err) => err,
@@ -197,3 +228,65 @@ fn verify_nested_property3() -> Result<(), Error> {
 
     Ok(())
 }
+
+#[test]
+fn verify_all_of_schema() -> Result<(), Error> {
+    let value = json!({
+        "prop1": "hello",
+        "prop3": "hello",
+        "another1": "another hello",
+    });
+    ALL_OF_SCHEMA_NO_ADDITIONAL
+        .verify_json(&value)
+        .expect("all of schema failed to verify valid object");
+
+    let value = json!({
+        "prop1": "hello",
+        "prop3": "hello",
+    });
+    test_verify(
+        &ALL_OF_SCHEMA_NO_ADDITIONAL,
+        &value,
+        &[("another1", "property is missing and it is not optional.")],
+    )?;
+
+    let value = json!({
+        "prop1": "hello",
+        "prop3": "hello",
+        "another1": "another hello",
+        "additional": "additional value",
+    });
+    test_verify(
+        &ALL_OF_SCHEMA_NO_ADDITIONAL,
+        &value,
+        &[("additional", "schema does not allow additional properties.")],
+    )?;
+
+    Ok(())
+}
+
+#[test]
+fn verify_all_of_schema_with_additional() -> Result<(), Error> {
+    let value = json!({
+        "prop1": "hello",
+        "prop3": "hello",
+        "regular1": "another hello",
+        "more": "additional property",
+    });
+    ALL_OF_SCHEMA_ADDITIONAL
+        .verify_json(&value)
+        .expect("all of schema failed to verify valid object");
+
+    let value = json!({
+        "prop1": "hello",
+        "prop3": "hello",
+        "more": "additional property",
+    });
+    test_verify(
+        &ALL_OF_SCHEMA_ADDITIONAL,
+        &value,
+        &[("regular1", "property is missing and it is not optional.")],
+    )?;
+
+    Ok(())
+}