def test__error_3():
    valids = [
        {
            'type': 'string',
            'regex': '0x[0-9a-f]{2}'
        },
        {
            'type': 'integer',
            'min': 0,
            'max': 255
        },
    ]
    v = Validator(schema={'foo': {'oneof': valids}})
    v.document = {'foo': '0x100'}
    v._error('foo', errors.ONEOF, (), 0, 2)
    error = v._errors[0]
    assert error.document_path == ('foo', )
    assert error.schema_path == ('foo', 'oneof')
    assert error.code == 0x92
    assert error.rule == 'oneof'
    assert error.constraint == valids
    assert error.value == '0x100'
    assert error.info == ((), 0, 2)
    assert error.is_group_error
    assert error.is_logic_error
示例#2
0
文件: cli.py 项目: rajivraj/RVD
def validate_file(filename, dump=False):
    """
    Auxiliary function, validate file

    :return dict representing the YAML document. NOTE that the
    returned dict hasn't removed any 'additional' key from the original
    file.
    """
    validated = False  # reflect whether the overall process suceeded
    cyan("Validating " + str(filename) + "...")
    doc = None
    try:
        with open(click.format_filename(filename), "r") as stream:
            try:
                doc = yaml.load(stream, Loader=yaml.FullLoader)
            except yaml.YAMLError as exception:
                raise exception
    except FileNotFoundError:
        red("File " + str(filename) + " not found")

    v = Validator(SCHEMA, allow_unknown=True)  # allow unknown values
    if doc:
        # print(MyNormalizer().normalized(doc, SCHEMA))
        if not v.validate(doc, SCHEMA):
            # print(v.errors)
            for key in v.errors.keys():
                print("\t" + str(key) + ": ", end="")
                red("not valid", end="")
                print(": " + str(v.errors[key]))
        else:
            # print(v.validated(doc))
            # valid_documents = [x for x in v.validated(doc)]
            # for document in valid_documents:
            #     print("\t" + str(document) + ": ", end='')
            #     green("valid")
            green("Validated successfully!")
            validated = True
    # else:
    #     red("file to validate not processed correctly!")

    if dump:
        # print(json.dumps(v.document, indent=4, default=default))
        # print(yaml.dump(v.document))  # reorder
        print(
            yaml.dump(v.document(), default_flow_style=False, sort_keys=False)
        )  # maintain order

        # flaw = Flaw(v.document)
        # print the final document after validations and normalizations
        # print(flaw)
        # print(flaw.yml())
    return validated, v.document
示例#3
0
def test__error_1():
    v = Validator(schema={'foo': {'type': 'string'}})
    v.document = {'foo': 42}
    v._error('foo', errors.BAD_TYPE, 'string')
    error = v._errors[0]
    assert error.document_path == ('foo',)
    assert error.schema_path == ('foo', 'type')
    assert error.code == 0x24
    assert error.rule == 'type'
    assert error.constraint == 'string'
    assert error.value == 42
    assert error.info == ('string',)
    assert not error.is_group_error
    assert not error.is_logic_error
示例#4
0
def test__error_2():
    v = Validator(schema={'foo': {'keyschema': {'type': 'integer'}}})
    v.document = {'foo': {'0': 'bar'}}
    v._error('foo', errors.KEYSCHEMA, ())
    error = v._errors[0]
    assert error.document_path == ('foo',)
    assert error.schema_path == ('foo', 'keyschema')
    assert error.code == 0x83
    assert error.rule == 'keyschema'
    assert error.constraint == {'type': 'integer'}
    assert error.value == {'0': 'bar'}
    assert error.info == ((),)
    assert error.is_group_error
    assert not error.is_logic_error
示例#5
0
def test__error_1():
    v = Validator(schema={'foo': {'type': 'string'}})
    v.document = {'foo': 42}
    v._error('foo', errors.TYPE, 'string')
    error = v._errors[0]
    assert error.document_path == ('foo',)
    assert error.schema_path == ('foo', 'type')
    assert error.code == 0x24
    assert error.rule == 'type'
    assert error.constraint == ('string',)
    assert error.value == 42
    assert error.info == ('string',)
    assert not error.is_group_error
    assert not error.is_logic_error
示例#6
0
def test__error_2():
    v = Validator(schema={'foo': {'keysrules': {'type': 'integer'}}})
    v.document = {'foo': {'0': 'bar'}}
    v._error('foo', errors.KEYSRULES, ())
    error = v._errors[0]
    assert error.document_path == ('foo',)
    assert error.schema_path == ('foo', 'keysrules')
    assert error.code == 0x83
    assert error.rule == 'keysrules'
    assert error.constraint == {'type': ('integer',)}
    assert error.value == {'0': 'bar'}
    assert error.info == ((),)
    assert error.is_group_error
    assert not error.is_logic_error
示例#7
0
def test__error_3():
    valids = [{'type': 'string', 'regex': '0x[0-9a-f]{2}'},
              {'type': 'integer', 'min': 0, 'max': 255}]
    v = Validator(schema={'foo': {'oneof': valids}})
    v.document = {'foo': '0x100'}
    v._error('foo', errors.ONEOF, (), 0, 2)
    error = v._errors[0]
    assert error.document_path == ('foo',)
    assert error.schema_path == ('foo', 'oneof')
    assert error.code == 0x92
    assert error.rule == 'oneof'
    assert error.constraint == valids
    assert error.value == '0x100'
    assert error.info == ((), 0, 2)
    assert error.is_group_error
    assert error.is_logic_error