yml验证

  • Post author:
  • Post category:其他




pkg_resources




获取文件的绝对路径
file_path = pkg_resources.resource_filename("package_name", "path")
  • 找到package_name的绝对路径
  • file_path = packe_name的绝对路径+path
  • 会判断package_name是否存在;不会判断file_path是否存在



pykwalify




语法规则


type

type 指定了数据结构中本节点的数据类型,其类型如下:

类型 描述
any 任何数据类型都可以
bool bool数据类型
date 日期型数据类型
float 浮点型数据
int 整型数据
mapping or mat 字典型数据
none None
number 整型或浮点型
scalar 除列表、字典、None以外的数据
str 字符串
text 字符串、整型、浮点型
timestamp 时间戳


Mapping

Mapping用于验证字典型数据结构:别名mapping、map

# Schema
type: map
mapping:
	key_one:
		type: str
# Schema
map:
	key_one:
		type: str
key_one: 'bar'


Sequence

Mapping用于验证列表数据类型

# Schema
type: seq
sequence:
  - type: str
# Schema
seq:
  - type: str
# Data
- 'Foobar'
- 'Barfoo'


Matching

序列块可以有多个子规则。给予一定的限制,序列块可以嵌套任意的深度。matching控制解析器处理序列块的子规则:

  • any

    每个序列项必须满足至少一个子规则
  • all

    每个序列项必须满足所有的子规则
  • *

    至少一个序列项必须满足至少一个子规则
# Schema
type: seq
matching: "any"
sequence:
  - type: str
  - type: seq
    sequence:
      - type: int
# Data
- - 123
- "foobar"


Required

如果指定required为true,则key和value必须指定,否则会报错。默认required为false

# Schema
type: map
mapping:
  key_one:
    type: str
    required: True
# Data
key_one: foobar


Enum

指定可能得元素,值必须是指定值值中的一个

# Schema
type: map
mapping:
  blood:
    type: str
    enum: ['A', 'B', 'O', 'AB']
# Data
blood: AB


pattern

指定值必须满足的正则表达式

# Schema
type: map
mapping:
  email:
    type: str
    pattern: .+@.+
# Data
email: foo@mail.com


Allowempty

只用于字典类型数据。如果指定为true,则字典可以出现出现语法文件中没有出现的key。但语法文件中指定的key必须出现并且其值也必须存在

# Schema
type: map
mapping:
  datasources:
    type: map
    allowempty: True
# Schema
# Data
datasources:
  test1: test1.py
  test2: test2.py


Extensions

Extensions 可用于个复杂的校验,其中函数的定义必须如下定义

def exp(value,rule_obj,path):
	pass
# Schema
extensions:
  - e.py
type: map
func: ext_map
mapping:
  foo:
    type: seq
    func: ext_list
    sequence:
      - type: str
        func: ext_str

e.py定义如下:

# -*- coding: utf-8 -*-
import logging
log = logging.getLogger(__name__)


def ext_str(value, rule_obj, path):
    log.debug("value: %s", value)
    log.debug("rule_obj: %s", rule_obj)
    log.debug("path: %s", path)

    # Either raise some exception that you have defined your self
    # raise AssertionError('Custom assertion error in jinja_function()')

    # Or you should return True/False that will tell if it validated
    return True


def ext_list(value, rule_obj, path):
    log.debug("value: %s", value)
    log.debug("rule_obj: %s", rule_obj)
    log.debug("path: %s", path)

    # Either raise some exception that you have defined your self
    # raise AssertionError('Custom assertion error in jinja_function()')

    # Or you should return True/False that will tell if it validated
    return True


def ext_map(value, rule_obj, path):
    log.debug("value: %s", value)
    log.debug("rule_obj: %s", rule_obj)
    log.debug("path: %s", path)

    # Either raise some exception that you have defined your self
    # raise AssertionError('Custom assertion error in jinja_function()')

    # Or you should return True/False that will tell if it validated
    return True


验证
from pykwalify.core import Core
c = Core(
        source_data=source_data,
        schema_files=[schema_file1, schema_file2],
        extensions=[schema_extensions],
    )
try:
    c.validate(raise_exception=True)
except SchemaError:
     raise YamlValidationException(
         "Please make sure the file is correct and all "
         "mandatory parameters are specified. Here are the errors "
         "found during validation",
         c.errors,
         content=source_data,
     )



版权声明:本文为weixin_42150936原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。