def set(self, key, value=None):

        if key == self.value_tag:
            if not isinstance(value, PY_STRING):
                raise GraphitValidationError(
                    '{0} should be of type "string" got "{1}"'.format(
                        key, type(value)), self)

            value = self.schema_validate(value)

            # String specific validation
            length = len(value)
            if length > self.get('maxLength', length):
                raise GraphitValidationError(
                    'Length of string {0} ({1}) larger then maximum {2}'.
                    format(value, length, self.get('maxLength')), self)
            if length < self.get('minLength', length):
                raise GraphitValidationError(
                    'Length of string {0} ({1}) smaller then minimum {2}'.
                    format(value, length, self.get('minLength')), self)
            # Regular expression pattern matching
            if self.get('pattern'):
                pattern = re.compile(self.get('pattern'))
                if not pattern.match(value):
                    raise GraphitValidationError(
                        'String {0} does not match regex pattern {1}'.format(
                            value, self.get('pattern')), self)

        self.nodes[self.nid][key] = value
    def set(self, key, value=None):

        if key == self.value_tag:
            if not isinstance(value, list):
                raise GraphitValidationError(
                    '{0} should be of type "array" got "{1}"'.format(
                        key, type(value)), self)

            value = self.schema_validate(value)

            # Array specific validation
            length = len(value)
            if length > self.get('maxItems', length):
                raise GraphitValidationError(
                    'Length of array {0} ({1}) larger then maximum {2}'.format(
                        key, length, self.get('maxItems')), self)
            if length < self.get('minItems', length):
                raise GraphitValidationError(
                    'Length of array {0} ({1}) smaller then minimum {2}'.
                    format(key, length, self.get('minItems')), self)
            if self.get('uniqueItems', False):
                if len(set(value)) > 1:
                    raise GraphitValidationError(
                        'Items in array {0} must be unique, got: {1}'.format(
                            key, set(value)), self)

        self.nodes[self.nid][key] = value
    def schema_validate(self, value):

        enum = self.get('enum')
        if enum and value not in enum:
            raise GraphitValidationError(
                '"{0}" should be of type {1}, got {2}'.format(
                    self.get(self.key_tag), repr(enum), value), self)

        return value
Esempio n. 4
0
    def set(self, key, value=None, **kwargs):

        if key == self.data.value_tag:
            if isinstance(value, PY_STRING) and UUID_REGEX.match(value):
                pass
            else:
                raise GraphitValidationError(
                    'No valid UUID: {0}'.format(value), self)

        self.nodes[self.nid][key] = value
    def set(self, key, value=None):

        if key == self.value_tag:
            if value not in (True, False):
                raise GraphitValidationError(
                    '{0} should be of type "boolean" got "{1}"'.format(
                        key, type(value)), self)

            value = self.schema_validate(value)

        self.nodes[self.nid][key] = value
    def set(self, key, value=None):

        if key == self.value_tag:
            if not isinstance(value, int):
                raise GraphitValidationError(
                    '{0} should be of type "integer" got "{1}"'.format(
                        key, type(value)), self)

            value = self.schema_validate(value)

            # Integer specific validation
            if value > self.get('maximum', value):
                raise GraphitValidationError(
                    '{0} is larger than maximum allowed {1}'.format(
                        value, self.get('maximum')), self)
            if value >= self.get('exclusiveMaximum', value + 1):
                raise GraphitValidationError(
                    '{0} is larger than maximum allowed {1}'.format(
                        value, self.get('exclusiveMaximum')), self)
            if value < self.get('minimum', value):
                raise GraphitValidationError(
                    '{0} is smaller than minimum allowed {1}'.format(
                        value, self.get('minimum')), self)
            if value <= self.get('exclusiveMinimum', value - 1):
                raise GraphitValidationError(
                    '{0} is larger than minimum allowed {1}'.format(
                        value, self.get('exclusiveMinimum')), self)
            if value != 0:
                if self.get('multipleOf', value) % value != 0:
                    raise GraphitValidationError(
                        '{0} is not a multiple of {1}'.format(
                            value, self.get('multipleOf')), self)

        self.nodes[self.nid][key] = value
Esempio n. 7
0
    def set(self, key=None, value=None):
        """
        Set and validate an ISO string representation of a date-time instance
        in accordance to RFC 3339.
        Set to current date-time if called without arguments.
        """

        key = key or self.data.value_tag
        if key == self.data.value_tag:
            if not value:
                value = self.now()
            dt = to_datetime(value, datetime)
            if dt:
                value = dt.astimezone(pytz.utc).isoformat()
            else:
                raise GraphitValidationError(
                    'Unsupported format for date-time: {0}'.format(
                        type(value)), self)

        self.nodes[self.nid][key] = value