Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic definitionPointer #14

Closed
s16508pjwstk opened this issue Aug 14, 2020 · 5 comments
Closed

Generic definitionPointer #14

s16508pjwstk opened this issue Aug 14, 2020 · 5 comments
Labels
invalid This doesn't seem right

Comments

@s16508pjwstk
Copy link

s16508pjwstk commented Aug 14, 2020

Hi, I've just implemented your validator and I have one problem.

Lets say I've got huge JSON swagger definition with multiple properties. Simplified example below.

  User:
    x-oneOf: [{required: ["id"]}, {required: ["name"]}]
    properties:
      id:
        type: integer
      name:
        type: string
  Data:
    properties:
      id:
        type: integer
      description:
        type: string
  History:
    properties:
      id:
        type: integer
      description:
        type: string

I want to perform a validation for whole JsonSchema printed above without specification which object of definitions should be validated.
Now, I have to run them explicitly

ProcessingReport report = validator.validate(jsonNode, "/definitions/User");
ProcessingReport report = validator.validate(jsonNode, "/definitions/Data");
ProcessingReport report = validator.validate(jsonNode, "/definitions/History");

Is there any possibility to pass definitionPointer argument which validates whole schema?.

Thank you in advance,
Kacper

@bjansen
Copy link
Owner

bjansen commented Aug 15, 2020

Hi, what you are trying to do is currently not possible. I'm not sure I understand your example though. Is a User also a Data and a History? The definitions are all the same in your example, so validating against any of them should be enough.

@s16508pjwstk
Copy link
Author

s16508pjwstk commented Aug 17, 2020

Yeah, that was a shortcut from my side.
Of course each of them is a different entity to be validated and each of them is a standalone object.

Maybe I'll try with something more concrete, let's say I have to validate object with nested validation rules. Object result is inside response object. How to perform a validation in this case to cover validation rules reference?

"Response" : {
      "type" : "object",
      "properties" : {
        "asyncRequestId" : {
          "type" : "string"
        },
        "beginTime" : {
          "type" : "string",
          "format" : "date-time"
        },
        "endTime" : {
          "type" : "string",
          "format" : "date-time"
        },
        "id" : {
          "type" : "string"
        },
        "name" : {
          "type" : "string"
        },
        "result" : {
          "$ref" : "#/definitions/AsyncResult"
        },
        "sceuid" : {
          "type" : "string"
        },
        "type" : {
          "type" : "string"
        },
        "userId" : {
          "type" : "string"
        },
        "version" : {
          "type" : "string"
        }
      }
}          

@bjansen
Copy link
Owner

bjansen commented Aug 17, 2020

It should work with ProcessingReport report = validator.validate(jsonNode, "/definitions/Response");.

It works fine with the following spec:

{
    "swagger": "2.0",
    "info": {
        "title": "Sample API",
        "version": "1"
    },
    "definitions": {
        "Response": {
            "type": "object",
            "required": [
                "asyncRequestId",
                "result"
            ],
            "properties": {
                "asyncRequestId": {
                    "type": "string"
                },
                "beginTime": {
                    "type": "string",
                    "format": "date-time"
                },
                "endTime": {
                    "type": "string",
                    "format": "date-time"
                },
                "id": {
                    "type": "string"
                },
                "name": {
                    "type": "string"
                },
                "result": {
                    "$ref": "#/definitions/AsyncResult"
                },
                "sceuid": {
                    "type": "string"
                },
                "type": {
                    "type": "string"
                },
                "userId": {
                    "type": "string"
                },
                "version": {
                    "type": "string"
                }
            }
        },
        "AsyncResult": {
            "type": "object",
            "properties": {
                "status": {
                    "type": "string"
                }
            }
        }
    }
}

and the following sample:

{
    "asyncRequestId": "qsdf",
    "result": {

    }
}

If the reference points to a definition located in an external file, the validation will fail (see #3).

@bjansen
Copy link
Owner

bjansen commented Aug 17, 2020

If I set status as required in AsyncResult and try to validate the same input, the processing report will contain an error:

error: object has missing required properties (["type"])
level: "error"
schema: {"loadingURI":"#","pointer":"/definitions/AsyncResult"}
instance: {"pointer":"/result"}
domain: "validation"
keyword: "required"
required: ["type"]
missing: ["type"]

-> it works as expected

@s16508pjwstk
Copy link
Author

Everything works perfectly, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants