question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Complex optional schema validation not working as expected

See original GitHub issue

Complex optional schema validation appears works as expected in json. However attempting to do the same in xml does not work as expected.

Feature:
  Scenario: JSON
    * def subSchema =
    """
    {
      c: #string
    }
    """
    * def schema =
    """
    {
      a: #string,
      b: ##(subSchema)
    }
    """
    * def test1 =
    """
    {
      a: x,
      b: {
        c: y
      }
    }
    """
    * match test1 == schema
    * def test2 =
    """
    {
      a: x
    }
    """
    * match test2 == schema

  Scenario: XML
    * def subSchema =
    """
    <c>#string</c>
    """
    * def schema =
    """
    <root>
      <a>#string</a>
      <b>##(subSchema)</b>
    </root>
    """
    * def test1 =
    """
    <root>
      <a>x</a>
      <b>
        <c>y</c>
      </b>
    </root>
    """
    * match test1 == schema
    * def test2 =
    """
    <root>
      <a>x</a>
    </root>
    """
    * match test2 == schema

In each case b is a “complex” object which is optional. With json, this field can either be present (and match the expected schema) or not present and the tests will pass. In xml, this does not work. Xml test2 fails with

path: /root, actual: '<root><a>x</a></root>', expected: '<root><a>#string</a><b><c>#string</c></b></root>', reason: all key-values did not match, expected has un-matched keys: [b]

In the xml example, it fails to recognise that b is optional.

If b were a “simple” object, then optional handling works as expected e.g. the following will pass

Feature:
  Scenario:
    * def schema =
    """
    <root>
      <a>#string</a>
      <b>##string</b>
    </root>
    """
    * def test1 =
    """
    <root>
      <a>x</a>
      <b>y</b>
    </root>
    """
    * match test1 == schema
    * def test2 =
    """
    <root>
      <a>x</a>
    </root>
    """
    * match test2 == schema

See the attached example project. This is comprised of the skeleton project (created using Maven quick-start) plus a test.feature file (the top code snippet) which demonstrates the issue. This was tested using karate version 0.9.6 and executed using the command mvn clean install myproject.zip

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ptrthomascommented, Dec 25, 2020

@theathlete3141 this is fixed

0reactions
ptrthomascommented, Mar 15, 2021

1.0 released

Read more comments on GitHub >

github_iconTop Results From Across the Web

Complex optional schema validation not working as expected
Complex optional schema validation appears works as expected in json. However attempting to do the same in xml does not work as expected....
Read more >
Multiple If-Then-Else not validating for JSON Schema
When I use this program to generate a schema for a smaller JSON object, the schema works correctly and validates as expected.
Read more >
Understanding JSON Schema
JSON Schema is a powerful tool for validating the structure of JSON data. However, learning to use it by reading its.
Read more >
Data validation with JSON Schema | Yehonathan Sharvit
In Data-Oriented Programming, we represent the request data in our program as a string map that is expected to have three fields: firstName...
Read more >
Validating Optional Objects with Yup - Object Partners
The simple version of the problem we were grappling with was this ... const yupSchema = { yup.object({ // We want color to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found