Skip to content

Some problems validating while checking "type", "pattern" and "format" #435

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

Closed
EwanLanegan opened this issue Jul 11, 2018 · 3 comments
Closed

Comments

@EwanLanegan
Copy link

Hey @Julian ! Loving the module so far. Having some problems whilst trying to validate a large JSON (the code below is an example, not the real thing). I am trying to validate JSON data against the following schema:

{	
    "$schema": "http://json-schema.org/draft-04/schema#"
    "title": "schema",
    "type": "object",
    "properties": {
        "key1": {"type": "string"}, 
        "key2": {"type": "number"}, 
        "key3": {"type": "string, "format": "email"}, 
        "key4": {"type": "string", "pattern": "[0-9]{4}"} 
    },
    "required": ["key1", "key2", "key3", "key4"],
    "additionalProperties": false
}

The problem is that the validation method is allowing any type of data to be input, and so is outputting false trues. I have read the docs but I cannot find a clear way to validate the types, patterns (especially) and formats. I think the main parts I may need advice on is FormatChecker() and TypeChecker(), but to be honest I am not sure where the problem is.

def testvalidatetypes(event, context):
    s3 = boto3.client("s3")
    bucket = "ewanstestbucket"                      #this just collects the schema from a storage system
    key = "schema2.json"
    schema = s3.get_object(Bucket=bucket,Key=key)

    
    json_data = {
        "key1": "Hello world",
        "key2": 42,
        "key3": "[email protected]",
        "key4": 42                        #this data should cause an error - should be 4 numbers, and a string
    }
    
    checker = jsonschema.FormatChecker()
    validator = jsonschema.Draft4Validator(schema, format_checker=checker)
    
    try:
        validator.validate(json_data)
        print("User data is valid.")
    except:
        print("User data is invalid!")
        for error in sorted(validator.iter_errors(json_data), key=str):
            print(error.message)

The output for this is User data is valid.
The output I need is User data is invalid! (type of error, where etc.)

I am new to this, so I have probably made a few really silly mistakes. However, if you could give a clear explanation of how to check if each type is the correct type, pattern and format, that would be great! I will test whatever you suggest immediately and get back to you.
Thanks :)

@Julian
Copy link
Member

Julian commented Jul 11, 2018

Hi @EwanLanegan, glad you're finding this useful!

In general I'd ask that you use StackOverflow, IRC, or Slack for support and reserve this issue tracker for bugs (or suspected bugs).

It's also extremely helpful regardless to minimize your error into a SSCCE, and there are a large number of things here that I suspect you can remove while still getting your error (at which point it may even be obvious what the issue is).

Because this is already here and there are a few obvious things:

  • Your schema is invalid JSON, it's missing things like a comma after the $schema value, an end quote on one of the "strings, etc. -- wouldn't recommend you hand-author JSON :), and certainly you need to validate it afterwards
  • You're passing a string into jsonschema.Draft4Validator, when it takes a schema, i.e. a dict, so you need to be calling json.loads on that (which would have pointed out the first thing)
  • Never use a bare except: :)

@Julian Julian closed this as completed Jul 11, 2018
@EwanLanegan
Copy link
Author

EwanLanegan commented Jul 11, 2018

@Julian Thanks for pointing these issues out to me! One question: Does the TypeChecker method need to be a parameter within the validate method, or is it a separate thing?

@Julian
Copy link
Member

Julian commented Jul 12, 2018

TypeChecker is new in 3.0.0 (which is imminent...). It's an argument to jsonschema.validators.extend you'd use if you wanted to extend an existing validator to customize the corresponding Python types used.

Julian added a commit that referenced this issue Oct 4, 2020
96742ba3 Merge pull request #438 from Zac-HD/overflow-with-multipleOf
c5ba4ba3 Check for multipleOf overflow
c12b0db8 Merge pull request #437 from gregsdennis/if-then-else-sequencing
bd148eb7 copy/paste error
3ca7c419 Added if/then/else sequencing tests; resolves #436
fa73bc8d Merge pull request #435 from jviotti/unevaluated-items-typo
2d6de7cb Fix "unevaluted" typos in "unevaluatedItems" suite
2a9be81d Merge pull request #1 from json-schema-org/master

git-subtree-dir: json
git-subtree-split: 96742ba3c4a1eff6de45f0c50a66a975796b7c37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants