如何验证一个列的值取决于另一个列的值,用于DBT。

huangapple go评论88阅读模式
英文:

How validate one column which values depend on another one for DBT

问题

我想在我的模型中添加测试以验证依赖于"TYPE"列的"subtype"值。

version: 2
models:
- name: my_table
description: my_table
columns:
- name: TYPE
description: "-"
tests:
- accepted_values:
values: ["1", "2", "3", "4"]
severity: error


现在的想法是验证"subtype",例如,如果"TYPE"是1,那么"subtype"只能是3或4,如果"TYPE"是2,那么"subtype"的允许值是1、5等等。
在此提前表示感谢。
英文:

I want to add test in my model to validate the values of subtype which depends on values of type column.

version: 2
models:
    - name: my_table
      description: my_table
      columns:
          - name: TYPE
            description: "-"
            tests:
                - accepted_values:
                      values: ["1", "2", "3", "4"]
                      severity: error

Now, the idea is validate subtype , where ,for example, if the type is 1, the subtype only could be 3 or 4, if the type is 2, the allowed values for subtype are 1, 5, etc.
It is possible, thanks in advance.

答案1

得分: 2

你应该能够向 subtype 添加不同的测试,以便当 type 等于某个值时,subtypeaccepted_values 应该是另一个值数组。

因此,例如:

version: 2
models:
    - name: my_table
      description: my_table
      columns:
          - name: TYPE
            description: "-"
            tests:
                - accepted_values:
                      values: ["1", "2", "3", "4"]
                      severity: error
          - name: SUBTYPE
            description: ""
            tests:
                - accepted_values:
                      name: subtype_accepted_values_when_type_equals_1
                      values: ["3", "4"]
                      config:
                          where: "type = 1"
                - accepted_values:
                      name: subtype_accepted_values_when_type_equals_2
                      values: ["1", "5"]
                      config:
                          where: "type = 2"
英文:

You should be able to add different tests to subtype, so that when type is equal to a value, the accepted_values of subtype should be another array of values.

So, for instance:

version: 2
models:
    - name: my_table
      description: my_table
      columns:
          - name: TYPE
            description: "-"
            tests:
                - accepted_values:
                      values: ["1", "2", "3", "4"]
                      severity: error
          - name: SUBTYPE
            description: ""
            tests:
                - accepted_values:
                      name: subtype_accepted_values_when_type_equals_1
                      values: ["3", "4"]
                      config:
                          where: "type = 1"
                - accepted_values:
                      name: subtype_accepted_values_when_type_equals_2
                      values: ["1", "5"]
                      config:
                          where: "type = 2"

huangapple
  • 本文由 发表于 2023年4月4日 16:51:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定