取决于Cerberus
中其他字段值的条件要求已经讨论了很多次。 dependencies
的使用不满足需求,因为在满足条件时可以unknown
字段。建议使用oneof
,但在满足多个条件和相关性的情况下,可能很难工作。现在有什么通用解决方案?
答案 0 :(得分:0)
cerberus
软件包支持“开箱即用”的复合验证。validation-success
,validation-fail
或validation-skipped
aadocuments = []
aadocuments.append(yaml.safe_load('''
person_fname: homer
person_lname: himpson
person_age: 33
prize_caption: free beer for life
prize_email: prizes@abcbooze.com
prize_category: alchohol
'''))
aadocuments.append(yaml.safe_load('''
person_fname: helen
person_lname: himpson
person_age: 16
prize_caption: free ammo for life
prize_email: prizes@zzzguns.com
prize_category: firearms
'''))
验证规则样本
- rule_caption: check-required-fields
rule_vpath: "@"
validation_schema:
person_fname:
type: string
required: true
person_lname:
type: string
required: true
person_age:
type: string
required: true
- rule_caption: check-age-range
rule_vpath: '@|@.person_age'
validation_schema:
person_age:
"min": 2
"max": 120
- rule_caption: check-underage-minor
rule_vpath: '[@]|[? @.person_age < `18`]'
validation_schema:
prize_category:
type: string
allowed: ['pets','toys','candy']
prize_email:
type: string
regex: '[\w]+@.*'
rule_vpath
名称-值对来指定触发条件。rule_vpath
,它告诉系统何时触发特定规则,这增加了对jmespath的依赖。