jpmckinney / validictory

🎓 deprecated general purpose python data validator
Other
240 stars 57 forks source link

Failing on required even when data not required #110

Closed chrigrahcisco closed 7 years ago

chrigrahcisco commented 7 years ago

It's been awhile since I've done any new work with Validictory and I'm hitting a blocker I need assistance with. I have a complex nested schema that has a single required field "templateId". But at least one (and possibly all) additional data will be included for CACHE, EMAIL, PUSH. If they are, each contains their own required properties.

I believe the schema is crafted properly to accommodate that logic. However, Validictory fails if any CACHE, EMAIL, PUSH is not in the data. I'm having difficulty determining what my options are to get Validictory test the data the way I need it to.

See the example below where EMAIL is missing in the data.

data = {
    "CACHE": {
        "body": "Sample text",
        "data": {
            "casenum": "Sample text"
        },
        "subject": "Sample text"
    },
    "PUSH": {
        "message": "Sample text",
        "payload": [
            {
                "nt": "Sample text"
            },
            {
                "cid": "Sample text"
            }
        ]
    },
    "applicationId": "Sample text",
    "templateId": "Sample text"
}

schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "description" : "Get Template Schema.",
    "properties": {
        "templateId": {
            "type":"string",
            "example":"SampleTemplate",
            "description":"The name of the template."
        },
        "applicationId": {
            "type":"string",
            "example":"CurrentApplication",
            "description":"The application which owns the template. This should always match the calling application."
        },
        "CACHE": {
            "type": "object",
            "description":"The CACHE delivery method for the template. At least one of EMAIL, CACHE or PUSH will be returned in the response.",
            "properties": {
                "body":{
                    "type":"string",
                    "description":"The body of the cache entry. This message can contain template substitution parameters."
                },
                "subject":{
                    "type":"string",
                    "description":"The subject of the cache entry. This subject can contain template substitution parameters."
                },
                "data":{
                    "type": "object",
                    "description":"The data object is a free form collection of string value pairs which can be populated in the cache entry. The values of these pairs can contain template substitution parameters. "
                },
                "cacheEntryExpiresAfterHours":{
                    "type":"integer",
                    "minimum":1,
                    "maximum":720,
                    "description": "The amount of time, in whole hours, after which this notification should be removed. The time must be at least 1 hour and cannot be longer than 720 hours (30 days)."
                }
            },
            "required":[
                "body",
                "subject",
                "data"
            ]
        },
        "EMAIL": {
            "type":"object",
            "description":"The EMAIL delivery method for the template. At least one of EMAIL, CACHE or PUSH will be returned in the response.",
            "properties":{
                "textMessage":{
                    "description":"The JSON encoded text message portion of the email. This string can contain template substituion parameters.",
                    "type":"string"
                },
                "subject":{
                    "description":"The JSON encoded subject portion of the email. This string can contain template substituion parameters.",
                    "type":"string"
                },
                "fromAddress":{
                    "description":"This email address that will show up as the from address on the resulting email. This should be the complete valid email address including the domain. This string is static and cannot contain template substitution parameters.",
                    "type":"string"
                },
                "htmlMessage":{
                    "description":"The JSON encoded html message portion of the email. This string can contain template substitution parameters.",
                    "type":"string"
                }
            },
            "required":[
                "textMessage",
                "subject",
                "fromAddress",
                "htmlMessage"
            ]
        },
        "PUSH":  {
            "type":"object",
            "description":"The PUSH delivery method for the template. At least one of EMAIL, CACHE or PUSH will be returned in the response.",
            "properties":{
                "message":{
                    "type":"string",
                    "description":"The message that will be sent as part of the push notification. This string can contain template substitution parameters."
                },
                "payload":{
                    "type": "array",
                    "description" : "Payload object array.",
                    "items": {
                        "type": "object",
                        "description" : "A string value pair that will be sent with the push notification. The value can contain template substitution parameters."
                    }
                }
            },
            "required": [
                "message",
                "payload"
            ]
        }
    },
    "required": [
        "templateId"
    ]
}
import validictory
validictory.validate(data, schema, fail_fast=False, required_by_default=False)

validictory.validator.MultipleValidationError: 1 validation errors:
Required field 'EMAIL' is missing
jamesturk commented 7 years ago

It looks like required logistics have changed in JSON schema in a backwards incompatible way between 03 and 04. Required was a boolean value until 04 when it was changed to an array.

Changing this to support 04 (or it looks like there's a new spec, 00) would break things for a lot of people, so validictory 1.0 at least will keep the existing behavior.

On Thu, Mar 9, 2017 at 3:20 PM, Christopher Graham <notifications@github.com

wrote:

It's been awhile since I've done any new work with Validictory and I'm hitting a blocker I need assistance with. I have a complex nested schema that has a single required field "templateId". But at least one (and possibly all) additional data will be included for CACHE, EMAIL, PUSH. If they are, each contains their own required properties.

I believe the schema is crafted properly to accommodate that logic. However, Validictory fails if any CACHE, EMAIL, PUSH is not in the data. I'm having difficulty determining what my options are to get Validictory test the data the way I need it to.

See the example below where EMAIL is missing in the data.

`data = { "CACHE": { "body": "Sample text", "data": { "casenum": "Sample text" }, "subject": "Sample text" }, "PUSH": { "message": "Sample text", "payload": [ { "nt": "Sample text" }, { "cid": "Sample text" } ] }, "applicationId": "Sample text", "templateId": "Sample text" }

schema = { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "description" : "Get Template Schema.", "properties": { "templateId": { "type":"string", "example":"SampleTemplate", "description":"The name of the template." }, "applicationId": { "type":"string", "example":"CurrentApplication", "description":"The application which owns the template. This should always match the calling application." }, "CACHE": { "type": "object", "description":"The CACHE delivery method for the template. At least one of EMAIL, CACHE or PUSH will be returned in the response.", "properties": { "body":{ "type":"string", "description":"The body of the cache entry. This message can contain template substitution parameters." }, "subject":{ "type":"string", "description":"The subject of the cache entry. This subject can contain template substitution parameters." }, "data":{ "type": "object", "description":"The data object is a free form collection of string value pairs which can be populated in the cache entry. The values of these pairs can contain template substitution parameters. " }, "cacheEntryExpiresAfterHours":{ "type":"integer", "minimum":1, "maximum":720, "description": "The amount of time, in whole hours, after which this notification should be removed. The time must be at least 1 hour and cannot be longer than 720 hours (30 days)." } }, "required":[ "body", "subject", "data" ] }, "EMAIL": { "type":"object", "description":"The EMAIL delivery method for the template. At least one of EMAIL, CACHE or PUSH will be returned in the response.", "properties":{ "textMessage":{ "description":"The JSON encoded text message portion of the email. This string can contain template substituion parameters.", "type":"string" }, "subject":{ "description":"The JSON encoded subject portion of the email. This string can contain template substituion parameters.", "type":"string" }, "fromAddress":{ "description":"This email address that will show up as the from address on the resulting email. This should be the complete valid email address including the domain. This string is static and cannot contain template substitution parameters.", "type":"string" }, "htmlMessage":{ "description":"The JSON encoded html message portion of the email. This string can contain template substitution parameters.", "type":"string" } }, "required":[ "textMessage", "subject", "fromAddress", "htmlMessage" ] }, "PUSH": { "type":"object", "description":"The PUSH delivery method for the template. At least one of EMAIL, CACHE or PUSH will be returned in the response.", "properties":{ "message":{ "type":"string", "description":"The message that will be sent as part of the push notification. This string can contain template substitution parameters." }, "payload":{ "type": "array", "description" : "Payload object array.", "items": { "type": "object", "description" : "A string value pair that will be sent with the push notification. The value can contain template substitution parameters." } } }, "required": [ "message", "payload" ] } }, "required": [ "templateId" ] }

import validictory validictory.validate(data, schema, fail_fast=False, required_by_default=False)

validictory.validator.MultipleValidationError: 1 validation errors: Required field 'EMAIL' is missing `

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jamesturk/validictory/issues/110, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAfYgVLbribxOeuE-DCK7wGtfRIJ7Kqks5rkF8TgaJpZM4MYlOp .

jamesturk commented 7 years ago

opened #113 to explore this and other changes related to latest jsonschema and if it is worth catching up