【问题标题】:Python cerberus – choices of stringsPython cerberus – 字符串的选择
【发布时间】:2019-12-02 08:58:47
【问题描述】:

如何验证字符串选择列表的架构?

假设我希望以下动物字符串有效:

['dog', 'cat', 'lion']

用于检查 animal 的键是否包含其中任何一个的架构是什么样的?

我不太清楚如何在这种情况下使用 anyOf 规则。

谢谢!

【问题讨论】:

    标签: python cerberus


    【解决方案1】:

    我认为allowed 规则更可取,因为它传达了目的:

    schema = {"animal": {"allowed": ["cat", "dog", "lion"]}}
    

    【讨论】:

      【解决方案2】:

      使用正则表达式怎么样?

      schema = {'foo': {'regex': r'(ham|spam)'}} 
      document = {'foo': 'ham'}
      v = Validator(schema) 
      v.validate(document)  # returns True
      

      【讨论】:

        猜你喜欢
        • 2012-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多