Adding a custom 'type'

476 views
Skip to first unread message

will...@gmail.com

unread,
Apr 10, 2018, 5:50:03 PM4/10/18
to jsonschema - An implementation of JSON Schema for Python
I'm not sure if this is possible, but I'm attempting to add a new 'type' called 'file', which validates the instance value by verifying that a file exists by that name.

I'm attempting to use the 'redefine' method of a TypeChecker to create a new 'type'.

import os

from jsonschema import validators, Draft4Validator


schema
= {
   
'type': 'file'
}

checker
= Draft4Validator.TYPE_CHECKER.redefine("file", lambda _, value: os.path.exist(value))
Validator = validators.extend(Draft4Validator, type_checker=checker)

Validator.check_schema(schema)

When I run this however, I get:

Traceback (most recent call last):
 
File "test_schema.py", line 24, in test_combination
   
Validator.check_schema(schema)
 
File "C:\python\lib\site-packages\jsonschema\validators.py", line 235, in check_schema
   
raise SchemaError.create_from(error)
jsonschema
.exceptions.SchemaError: 'file' is not valid under any of the given schemas

Failed validating 'anyOf' in metaschema['properties']['type']:
   
{'anyOf': [{'$ref': '#/definitions/simpleTypes'},
               
{'items': {'$ref': '#/definitions/simpleTypes'},
               
'minItems': 1,
               
'type': 'array',
               
'uniqueItems': True}]}

It looks like redefine isn't the right way to go about this, is there another way to add a custom type?

Julian Berman

unread,
Apr 10, 2018, 7:32:37 PM4/10/18
to will...@gmail.com, jsonschema - An implementation of JSON Schema for Python
In general the metaschemas these days restrict the types to just the predefined names -- so TypeCheckers are mainly for changing the mapping to the existing types more than adding new ones.

What you'd need to do here is to also add the file type to the metaschema you're extending, or alternatively, using "format" instead of "type" for the custom behavior is a bit smoother for these sorts of things.

--
You received this message because you are subscribed to the Google Groups "jsonschema - An implementation of JSON Schema for Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema+...@googlegroups.com.
To post to this group, send email to jsons...@googlegroups.com.

will...@gmail.com

unread,
Apr 11, 2018, 8:31:36 AM4/11/18
to jsonschema - An implementation of JSON Schema for Python
I haven't tried the 'format' option yet, but your recommendation about adding to the metaschema seems to have fixed it:

from os.path import exists

from jsonschema import validators, Draft4Validator


schema
= {'type': 'file'}
Draft4Validator.META_SCHEMA['definitions']['simpleTypes']['enum'].append('file')

checker
= Draft4Validator.TYPE_CHECKER.redefine("file", lambda _, value: exists(value))

Validator = validators.extend(Draft4Validator, type_checker=checker)
Validator.check_schema(schema)


validator
= Validator(schema=schema)
validator
.validate('real.txt')
validator
.validate('fake.txt')

Traceback (most recent call last):

 
File "test_schema.py", line 21, in test_combination
    validator
.validate('fake.txt')
 
File "C:\python\lib\site-packages\jsonschema\validators.py", line 282, in validate
   
raise error
jsonschema
.exceptions.ValidationError: 'fake.txt' is not of type 'file'

Failed validating 'type' in schema:
   
{'type': 'file'}

On instance:
   
'fake.txt'

Thanks!

Michał Stolarczyk

unread,
Feb 21, 2020, 5:19:38 PM2/21/20
to jsonschema - An implementation of JSON Schema for Python
hi,

How do I use the new custom Validator with jsonschema.validate function?

This is what I have:

from os.path import exists
import jsonschema

def f(checker, instance):
    return exists(instance)
jsonschema.Draft6Validator.META_SCHEMA['definitions']['simpleTypes']['enum'].append('path')
checker = jsonschema.Draft6Validator.TYPE_CHECKER.redefine("path", f)

myValidator = validators.extend(jsonschema.Draft6Validator, type_checker=checker)
pvalidator = myValidator(schema={'type': 'path'})
schema = _read_schema("/path/to/schema.yaml")  
jsonschema.validate(p2, schema, cls= pvalidator)

calling the function results in a TypError: 

TypeError: 'Validator' object is not callable



Thanks in advance!

Michal
Reply all
Reply to author
Forward
0 new messages