コード例 #1
0
ファイル: validation.py プロジェクト: ajufrancis/exabgp
def _reference (root,references,json,location):
	if not references:
		return

	ref = references if check.array(references) else [references,]
	jsn = json if check.array(json) else json.keys() if check.object(json) else [json,]

	valid = []
	for reference in ref:
		compare = root
		for path in reference.split(','):
			compare = compare.get(path,{})
		# prevent name conflict where we can not resolve which object is referenced.
		add = compare.keys()
		for k in add:
			if k in valid:
				raise ValidationError(location, "duplicate reference in " % ', '.join(references))

				return False
		valid.extend(add)

	for option in jsn:
		if not option in valid:
			destination = ' or '.join(references) if type(references) == type ([]) else references
			raise ValidationError(location, "the referenced data in %s is not present" % destination)

	return True
コード例 #2
0
def _reference (root,references,json,location):
	if not references:
		return

	ref = references if check.array(references) else [references,]
	jsn = json if check.array(json) else json.keys() if check.object(json) else [json,]

	valid = []
	for reference in ref:
		compare = root
		for path in reference.split(','):
			compare = compare.get(path,{})
		# prevent name conflict where we can not resolve which object is referenced.
		add = compare.keys()
		for k in add:
			if k in valid:
				raise ValidationError(location, "duplicate reference in " % ', '.join(references))

				return False
		valid.extend(add)

	for option in jsn:
		if not option in valid:
			destination = ' or '.join(references) if type(references) == type ([]) else references
			raise ValidationError(location, "the referenced data in %s is not present" % destination)

	return True
コード例 #3
0
ファイル: validation.py プロジェクト: ajufrancis/exabgp
def _validate (root,json,definition,location=[]):
	kind,presence,references,contextual = definition

	# ignore missing optional elements
	if not json:
		if presence == PRESENCE.mandatory:
			raise ValidationError(location, ValidationError.mandatory_error)
		return

	# check that the value of the right type
	if not check.kind(kind,json):
		raise ValidationError(location, ValidationError.type_error)

	# for object check all the elements inside
	if kind & TYPE.object and check.object(json):
		subdefinition = contextual
		keys = deque(subdefinition.keys())

		while keys:
			key = keys.popleft()
			if DEBUG: print "  "*len(location) + key

			if key.startswith('_'):
				continue

			if type(json) != type({}):
				raise ValidationError(location, ValidationError.type_error)

			if key == '<*>':
				keys.extendleft(json.keys())
				continue

			_reference (root,references,json,location)

			star = subdefinition.get('<*>',None)
			subtest = subdefinition.get(key,star)
			if subtest is None:
				raise ValidationError(location, ValidationError.configuration_error)
			_validate(root,json.get(key,None),subtest,location + [key])

	# for list check all the element inside
	elif kind & TYPE.array and check.array(json):
		test = contextual
		# This is a function
		if hasattr(test, '__call__'):
			for data in json:
				if not test(data):
					raise ValidationError(location, ValidationError.type_error)
		# This is a list of valid option
		elif type(test) == type([]):
			for data in json:
				if not data in test:
					raise ValidationError(location, ValidationError.type_error)
		# no idea what the data is - so something is wrong with the program
		else:
			raise ValidationError(location,ValidationError.internal_error)

	# for non container object check the value
	else:
		test = contextual
		# check that the value of the data
		if hasattr(test, '__call__'):
			if not test(json):
				raise ValidationError(location, ValidationError.type_error)
		# a list of valid option
		elif type(test) == type([]):
			if not json in test:
				raise ValidationError(location, ValidationError.type_error)
		else:
			raise ValidationError(location,ValidationError.internal_error)

	_reference (root,references,json,location)
コード例 #4
0
def _validate(root, json, definition, location=()):
    kind, presence, references, contextual = definition

    # ignore missing optional elements
    if not json:
        if presence == PRESENCE.mandatory:
            raise ValidationError(location, ValidationError.mandatory_error)
        return

    # check that the value of the right type
    if not check.kind(kind, json):
        raise ValidationError(location, ValidationError.type_error)

    # for object check all the elements inside
    if kind & TYPE.object and check.object(json):
        subdefinition = contextual
        keys = deque(subdefinition.keys())

        while keys:
            key = keys.popleft()
            if DEBUG: print "  " * len(location) + key

            if key.startswith('_'):
                continue

            if type(json) != type({}):
                raise ValidationError(location, ValidationError.type_error)

            if key == '<*>':
                keys.extendleft(json.keys())
                continue

            _reference(root, references, json, location)

            star = subdefinition.get('<*>', None)
            subtest = subdefinition.get(key, star)
            if subtest is None:
                raise ValidationError(location,
                                      ValidationError.configuration_error)
            _validate(root, json.get(key, None), subtest, location + [key])

    # for list check all the element inside
    elif kind & TYPE.array and check.array(json):
        test = contextual
        # This is a function
        if hasattr(test, '__call__'):
            for data in json:
                if not test(data):
                    raise ValidationError(location, ValidationError.type_error)
        # This is a list of valid option
        elif type(test) == type([]):
            for data in json:
                if not data in test:
                    raise ValidationError(location, ValidationError.type_error)
        # no idea what the data is - so something is wrong with the program
        else:
            raise ValidationError(location, ValidationError.internal_error)

    # for non container object check the value
    else:
        test = contextual
        # check that the value of the data
        if hasattr(test, '__call__'):
            if not test(json):
                raise ValidationError(location, ValidationError.type_error)
        # a list of valid option
        elif type(test) == type([]):
            if not json in test:
                raise ValidationError(location, ValidationError.type_error)
        else:
            raise ValidationError(location, ValidationError.internal_error)

    _reference(root, references, json, location)