def _validate_object(self, obj, schema=None): """ """ if obj in (None, NO_VALUE, EMPTY_STRING): return True if isinstance(obj, six.string_types): raise WrongType('value must be json serialable!') try: # Test if value is iterable iter(obj) json.dumps(obj) except (TypeError, ValueError) as exc: msg = _('Only dict or list type value is allowed, value must be json serialable!') if api.env.debug_mode(): msg += _('Original exception: {0!s}').format(exc) six.reraise(WrongType, WrongType(msg), sys.exc_info()[2]) if schema: try: jsonschema.validate(obj, schema) except ( jsonschema.ErrorTree, jsonschema.ValidationError, jsonschema.SchemaError, jsonschema.FormatError, jsonschema.RefResolutionError ) as exc: six.reraise(Invalid, Invalid(str(exc)), sys.exc_info()[2])
def _validate_fields(self, raw_data): errors = [] for name, field, schema in self.fields: if name not in raw_data: continue field = field.bind(self.obj or self.model) raw_value = raw_data.pop(name) if isinstance(raw_value, str): raw_value = raw_value.decode('utf8') # We don't want to accidentally swallow any adaptation TypeErrors from here: from_unicode = IFromUnicode(field) try: if raw_value in (None, u'', '') and field.required: raise RequiredMissing(name) try: value = from_unicode.fromUnicode(raw_value) except (ValueError, TypeError): raise WrongType(name) # TODO: make this more descriptive as to which validation failed, # where was it defined etc. except zope.schema.ValidationError as exc: errors.append((name, exc)) else: setattr(self.adapted_tmp_obj(self.tmp_obj, schema), name, value) return errors
def _validate_object(self, obj, schema=None): """" """ res = super(JSONObjectValue, self)._validate_object(obj, schema) if not res: if isinstance(obj, (list, tuple, set)): raise WrongType('given value must be dict type data but got `{0}` type data!'.format(type(obj))) return res
def create_application(factory, container, name): """Creates an application and triggers the events from the application lifecycle. """ # Check the factory. if not IApplication.implementedBy(factory): raise WrongType(factory) # Check the availability of the name in the container. if name in container: raise KeyError(name) # Instanciate the application application = factory() # Trigger the creation event. notify(ObjectCreatedEvent(application)) # Persist the application. # This may raise a KeyError. container[name] = application # Trigger the initialization event with the new application as a # current site. current = getSite() setSite(application) try: notify(ApplicationAddedEvent(application)) finally: setSite(current) return application
def _validate(self, value): if self.allowed_mime_types\ and value.mimeType not in self.allowed_mime_types: raise WrongType(value, self.allowed_mime_types) if self.max_length is not None and len(value.raw) > self.max_length: raise Invalid( _('msg_text_too_long', default=u'Text is too long. (Maximum ${max} characters.)', mapping={'max': self.max_length})) if not self.constraint(value): raise ConstraintNotSatisfied(value)
def patch(self, patch_data): """:@links: https://python-json-patch.readthedocs.io/en/latest/tutorial.html#creating-a-patch""" if not isinstance(patch_data, (list, tuple)): raise WrongType('patch value must be list or tuple type! but got `{0}` type.'.format(type(patch_data))) try: patcher = jsonpatch.JsonPatch(patch_data) # Pacther is doing deepcopy! new_value = patcher.apply(self) # Let's update self.update(new_value) except (jsonpatch.JsonPatchException, jsonpatch.JsonPointerException) as e: six.reraise(Invalid, Invalid(str(e)), sys.exc_info()[2])
def init_validate(self): """ """ schema = self.json_schema if schema is None: # No validation is required. return try: if isinstance(schema, six.string_types): schema = json.loads(schema) self.json_schema = schema json.dumps(schema) if not isinstance(schema, dict): raise WrongType( 'Schema value must be dict type! but got `{0!s}` type'.format(type(self.json_schema))) except (ValueError, TypeError) as exc: msg = _('Invalid schema data type! dict data type is expected.') if api.env.debug_mode(): msg += _('Original Exception: {0!s}').format(exc) six.reraise(Invalid, Invalid(msg), sys.exc_info()[2])