Пример #1
0
    def test_lib_hashitem(self):
        x = {
            'foo':['bar','baz'],
            'boing':('gniob','boing'),

            'lol':10,
            'hehe':'haha',
            'gronk':{
                'hurr':30,
                'durr':40,
            },
        }

        y = {
            'boing':('gniob','boing'),
            'foo':['bar','baz'],

            'lol':10,
            'gronk':{
                'durr':40,
                'hurr':30,
            },
            'hehe':'haha',
        }

        self.eq( s_hashitem.hashitem(x), s_hashitem.hashitem(y) )
Пример #2
0
def getJsValidator(schema):
    '''
    Get a fastjsonschema callable.

    Args:
        schema (dict): A JSON Schema object.

    Returns:
        callable: A callable function that can be used to validate data against the json schema.
    '''
    if schema.get('$schema') is None:
        schema['$schema'] = 'http://json-schema.org/draft-07/schema#'

    # It is faster to hash and cache the functions here than it is to
    # generate new functions each time we have the same schema.
    key = s_hashitem.hashitem(schema)
    func = _JsValidators.get(key)
    if func:
        return func

    func = fastjsonschema.compile(schema)

    def wrap(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except JsonSchemaException as e:
            raise s_exc.SchemaViolation(mesg=e.message, name=e.name)

    _JsValidators[key] = wrap
    return wrap