Search code examples
marklogicjsonschemajson-schema-validatormarklogic-optic-apimarklogic-11

JSON schema validation for number types in MarkLogic


I am using xdmp:json-validate-node function to validate json document before inserting it to MarkLogic. but seems json schema validation does not raise error when number typed field has NaN value. I have defined TDE on my json documents which reject my document because in TDE I have defined scalar-type as decimal.

Is there any way to validate json-schema for NaN values for Number type?

I tried below query in MarkLogic qconsole. I am expecting validation error but it gives back input json

xquery version "1.0-ml";

let $node := object-node {
  "name": "test",
  "age": fn:number("fail")
  
}
let $schema := object-node {
  "properties": object-node {
    "name": object-node {  
      "type": "string"
    }, 
    "age": object-node{
        "type": "number"
    }
  }
}
return xdmp:json-validate-node($node, $schema, "full")

Solution

  • NaN is a number, but actually isn't allowed in JSON.

    There has been some debate about whether or not it should be valid, and whether to have an option to configure the behavior for json-schema.

    The current implementation in MarkLogic will treat NaN as a number and is seen as valid, and I don't believe there is a way to configure the behavior with options. So, you would need to augment your validation with something custom if you want to flag it as invalid.

    I would suggest opening a MarkLogic Support ticket requesting enhancements to the JSON Validation implementation and/or submit a feature request on the MarkLogic Ideas portal.