def validate(self, spec): if not isinstance(spec, list): raise InvalidSpecException( "Input spec to spec engine must be a list, but is type {}".format( type(spec) ) ) for item_spec in spec: if not isinstance(item_spec, dict): raise InvalidSpecException( "Items in input spec to engine must be dict, but is type {}".format( type(item_spec) ) ) resource_type = item_spec.get("type") if not resource_type: raise InvalidSpecException( "Items in input spec must contain key/value item for 'type:'" ) resource_spec = item_spec.get("resource_spec") if not resource_spec: raise InvalidSpecException( "Items in input spec must contain key/value item for 'resource_spec:'" ) subclass = get_resource_subclass(resource_type) if not subclass: raise ResourceTypeNotFoundException( "Resource type {} was not found".format(resource_type) ) subclass.validate(resource_spec)
def validate(cls, input, spec=None): """Validates that an input dictionary spec is valid according to a provided class spec. Recursively walksdown and checks if all required attributes are present, and attribute types match spec types. Returns complete spec with all attributes. """ valid_spec = {} if not isinstance(input, dict): raise InvalidSpecException("input spec was not a dictionary") if not spec: spec = cls.spec for k, v in input.items(): if not spec.get(k): raise InvalidSpecException( "Unknown property found: {}".format(k)) for k, v in spec.items(): property_name = k property_type = v.get("type") property_required = v.get("required") property_default = v.get("default", default_empty_value(property_type)) input_property = validate_property(input, property_name, property_required, property_default, property_type) if (property_type == dict and input_property != property_default and v.get("nested")): property_value = cls.validate(input_property, v.get("nested")) elif property_type == list: list_property_type = v.get("list_item") list_spec = [] for item in input_property: if type(item) != list_property_type: raise InvalidSpecException( "list item {} not match type {}".format( item, list_property_type)) if list_property_type == str: list_spec.insert(0, item) else: list_spec.insert( 0, cls.validate(item, v.get("nested", {}))) property_value = list_spec else: property_value = input_property valid_spec[property_name] = property_value return valid_spec
def validate_spec_item(cls, property_name, property_item, input, spec): property_type = property_item.get("type") property_required = property_item.get("required") property_default = property_item.get("default", default_empty_value(property_type)) input_property = validate_property( input, property_name, property_required, property_default, property_type ) if ( property_type == dict and input_property != property_default and property_item.get("nested") ): property_value = cls.validate(input_property, property_item.get("nested")) elif property_type == list: list_property_type = property_item.get("list_item") list_spec = [] for item in input_property: if type(item) != list_property_type: raise InvalidSpecException( "list item {} not match type {}".format( item, list_property_type ) ) if list_property_type == str: list_spec.insert(0, item) else: list_spec.insert(0, cls.validate(item, property_item.get("nested", {}))) property_value = list_spec else: property_value = input_property return property_value
def validate_property(input_spec, property_name, property_required, property_default, property_type): input_property = input_spec.get(property_name) if not input_property: if property_required: raise InvalidSpecException( "required property {} not found in input spec".format( property_name)) else: input_property = property_default elif type(input_property) != property_type: raise InvalidSpecException( "input property {} not match type {}".format( property_name, property_type)) return input_property
def validate_spec_properties(input_spec, spec): for k, v in input_spec.items(): if not spec.get(k): raise InvalidSpecException("Unknown property found: {}".format(k))
def validate_spec_type(input_spec): if not isinstance(input_spec, dict): raise InvalidSpecException("input spec was not a dictionary")