Pub.schema:validate using document type

I’m trying to validate a document using pub.schema:validate. Here is a representative doc type definition (named ABC):

ID (string - required, no null)
Name (string - optional)
Summary (document list - required, no null)
…Product (string - required, no null)
…Type (string - required, no null)
Stage (string - required, no null)

Document var xyz has these fields present:

ID - 123
Name - foo
Stage - bar

Calling pub.schema:validate with these inputs:

object - xyz
conformsTo - ABC (IS doc type, not a schema)
maxErrors - -1
failIfInvalid - true

I expect the call to fail since xyz does not have the required doc list “Summary” present, but it does not. It fails as expected if “Stage” is omitted.

I’m missing something simple, I’m sure. Does anyone have an idea of what I’m overlooking?

I think you must inspect the conformsTo value you are setting, this value affects the result:

Hope this give you a hint :smiley:

Hi Reamon,

How are you creating IS Doc type to pass it into conformsTo field?
conformsTo is a String field, How can I pass a Doc type to string field.

Here is my json schema:

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "$ref": "#/definitions/Welcome",
    "definitions": {
        "Welcome": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "header": {
                    "$ref": "#/definitions/header"
                },
                "body": {
                    "$ref": "#/definitions/body"
                }
            },
            "required": [
                "body",
                "header"
            ],
            "title": "Welcome"
        },
        "body": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "phonenumber": {
                    "type": "string",
                    "pattern": "^[\\d]{9}$"
                }
            },
            "required": [
                "phonenumber"
            ]
        },
        "header": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "Id": {
                    "type": "string",
                    "pattern": "^\\d{10}$"
                }
            },
            "required": [
                "Id"
            ]
            
        }
    }
}

when I pass in the schema directly into conformTo field, it throws this error
com.wm.app.b2b.server.ServiceException: JSON schema validation failed:{} is not a valid JSON document type.
Please let me know.

Thanks

Pass in the name of the IS document type, not the JSON schema string. In Designer, create a “JSON Document Type”. You may need to adjust your JSON schema. When I tried to do this using the JSON above, it fails due to multiple issues.

Once the doc type is created, call pub.json.schema:validate to validate JSON content (string, bytes, stream, IS document) against the JSON document type. Pass the document type name that you created, not the JSON schema string.

Side note: I would encourage that you NOT perform document validation “in the middle.” Typically it is preferable to have the end points do that, if needed, because they know what they really need/care about which can change over time. What usually happens is middleware flags a document as invalid, then the end point application team says “oh, ignore that.” :slight_smile:

HTH.

You the man boss. Worked smoothly.
Appreciate your help very much.
Thanks alot.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.