def _items( *, value, schema: dict, uri: str, validation: Validation, ref_list: RefList ) -> Callback: """ items https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.4.1 https://json-schema.org/draft-06/json-schema-validation.html#rfc.section.6.9 """ # TODO base of value's type, there are 2 ways to check ref_list if isinstance(value, list): inner_callbacks = [] for subschema in value: inner_callbacks.append(validation.evaluate(subschema, uri, ref_list)) def callback(instance): for inner_callback, subinstance in zip(inner_callbacks, instance): if inner_callback(subinstance) is False: return False return True return callback else: inner_callback = validation.evaluate(value, uri, ref_list) def callback(instance): for subinstance in instance: if inner_callback(subinstance) is False: return False return True return callback
def _additional_properties( *, value, schema: dict, uri: str, validation: Validation, ref_list: RefList ) -> Callback: """ additionalProperties https://json-schema.org/draft-06/json-schema-validation.html#rfc.section.6.20 https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.5.6 """ f = validation.evaluate(value, uri, ref_list) def callback(instance): for key, subinstance in instance.items(): if key in schema.get("properties", {}): continue match_pattern = False for key_pattern in schema.get("patternProperties", {}): if re.search(key_pattern, key): match_pattern = True break if match_pattern: continue result = f(subinstance) if result is False: return False return True return callback
def _property_names( *, value, schema: dict, uri: str, validation: Validation, ref_list: RefList ) -> Callback: """ propertyNames https://json-schema.org/draft-06/json-schema-validation.html#rfc.section.6.22 https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.5.8 """ inner_callback = validation.evaluate(value, uri, []) return lambda instance: all([inner_callback(i) for i in cast(dict, instance)])
def _not( *, value, schema: dict, uri: str, validation: Validation, ref_list: RefList ) -> Callback: """ not https://json-schema.org/draft-06/json-schema-validation.html#rfc.section.6.29 https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.7.4 """ assert isinstance(value, (bool, dict)) inner_callback = validation.evaluate(value, uri, ref_list) return lambda instance: not inner_callback(instance)
def _if( *, value, schema: dict, uri: str, validation: Validation, ref_list: RefList ) -> Callback: """ if https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.6.1 """ if_callback = validation.evaluate(value, uri, ref_list) then_callback = validation.evaluate(schema.get("then", True), uri, ref_list) else_callback = validation.evaluate(schema.get("else", True), uri, ref_list) def callback(instance) -> bool: if_result = if_callback(instance) if if_result: then_result = then_callback(instance) return then_result else: else_result = else_callback(instance) return else_result return callback
def _any_of( *, value, schema: dict, uri: str, validation: Validation, ref_list: RefList ) -> Callback: """ anyOf https://json-schema.org/draft-06/json-schema-validation.html#rfc.section.6.27 https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.7.2 """ assert not ref_list inner_callbacks = [] for subschema in value: inner_callbacks.append(validation.evaluate(subschema, uri, ref_list)) return lambda instance: any([f(instance) for f in inner_callbacks])
def _contains( *, value, schema: dict, uri: str, validation: Validation, ref_list: RefList ) -> Callback: """ contains https://json-schema.org/draft-06/json-schema-validation.html#rfc.section.6.14 https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.4.6 """ inner_callback = validation.evaluate(value, uri, ref_list) def callback(instance): for subinstance in instance: if inner_callback(subinstance) is True: return True return False return callback
def _one_of( *, value, schema: dict, uri: str, validation: Validation, ref_list: RefList ) -> Callback: """ oneOf https://json-schema.org/draft-06/json-schema-validation.html#rfc.section.6.28 https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.7.3 """ assert not ref_list inner_callbacks = [] for subschema in value: inner_callbacks.append(validation.evaluate(subschema, uri, ref_list)) def callback(instance) -> bool: count = 0 for inner_callback in inner_callbacks: if inner_callback(instance) is True: count += 1 return count == 1 return callback
def _properties( *, value, schema: dict, uri: str, validation: Validation, ref_list: RefList ) -> Callback: """ properties https://json-schema.org/draft-06/json-schema-validation.html#rfc.section.6.18 https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.5.4 """ assert not ref_list inner_callbacks = {} for key, subschema in value.items(): inner_callbacks[key] = validation.evaluate(subschema, uri, ref_list) def final_callback(instance): for key, inner_callback in inner_callbacks.items(): if key in instance: if inner_callback(instance[key]) is False: return False return True return final_callback
def _additional_items( *, value, schema: dict, uri: str, validation: Validation, ref_list: RefList ) -> Callback: """ additionalItems https://json-schema.org/draft-06/json-schema-validation.html#rfc.section.6.10 https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.4.2 """ inner_callback = validation.evaluate( value, uri, ref_list ) # TODO is ref_list correct? def callback(instance): if isinstance(schema.get("items"), list): if len(schema["items"]) < len(instance): for subinstance in instance[len(schema["items"]) :]: if inner_callback(subinstance) is False: return False return True return callback
def _dependencies( *, value, schema: dict, uri: str, validation: Validation, ref_list: RefList ) -> Callback: """ dependencies https://json-schema.org/draft-06/json-schema-validation.html#rfc.section.6.21 https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.5.7 """ assert isinstance(value, dict) inner_callbacks = [] for dependency_key, dependency_value in value.items(): if isinstance(dependency_value, list): assert all([isinstance(i, str) for i in dependency_value]) def inner_callback(instance): if dependency_key in instance: for key in dependency_value: if key not in instance: return False return True inner_callbacks.append(inner_callback) else: assert isinstance(dependency_value, (dict, bool)) dependency_callback = validation.evaluate(dependency_value, uri, ref_list) def inner_callback(instance): if dependency_key in instance: if dependency_callback(instance) is False: return False return True inner_callbacks.append(inner_callback) return lambda instance: all([f(instance) for f in inner_callbacks])
def _pattern_properties( *, value, schema: dict, uri: str, validation: Validation, ref_list: RefList ) -> Callback: """ patternProperties https://json-schema.org/draft-06/json-schema-validation.html#rfc.section.6.19 https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.5.5 """ inner_callbacks = {} assert not ref_list for key_pattern, subschema in value.items(): inner_callbacks[key_pattern] = validation.evaluate(subschema, uri, ref_list) def callback(instance): for key_pattern, inner_callback in inner_callbacks.items(): for key, subinstance in instance.items(): if re.search(key_pattern, key): if inner_callback(subinstance) is False: return False return True return callback