|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: Using Multiple [If] Attributes in a Single Group |
| 4 | +bookmark: Stacking Ifs |
| 5 | +permalink: /schema/examples/:title/ |
| 6 | +icon: fas fa-tag |
| 7 | +order: "01.5.2.5" |
| 8 | +--- |
| 9 | +This example shows how multiple `[If]` attributes can be combined to create "OR" logic. |
| 10 | + |
| 11 | +Suppose we have the following model |
| 12 | + |
| 13 | +```c# |
| 14 | +class CarColors |
| 15 | +{ |
| 16 | + public string Interior { get; set; } |
| 17 | + public string Exterior { get; set; } |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +Now let's say that there is a requirement that if the exterior color is white or black, the interior needs to be gray; otherwise, the interior can be any other color. |
| 22 | + |
| 23 | +## The standard way |
| 24 | + |
| 25 | +In normal usage, we could create two condition groups: one for white and one for black. We would the apply constraints under those conditions. |
| 26 | + |
| 27 | +```c# |
| 28 | +[If(nameof(Exterior, "white", "isWhite"))] |
| 29 | +[If(nameof(Exterior, "black", "isBlack"))] |
| 30 | +class CarColors |
| 31 | +{ |
| 32 | + [Const("gray", ConditionGroup = "isWhite")] |
| 33 | + [Const("gray", ConditionGroup = "isBlack")] |
| 34 | + public string Interior { get; set; } |
| 35 | + public string Exterior { get; set; } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +This would create the schema: |
| 40 | + |
| 41 | +```json |
| 42 | +{ |
| 43 | + "type": "object", |
| 44 | + "properties": { |
| 45 | + "Interior": { "type": "string" }, |
| 46 | + "Exterior": { "type": "string" } |
| 47 | + }, |
| 48 | + "allOf": [ |
| 49 | + { |
| 50 | + "if": { |
| 51 | + "properties": { |
| 52 | + "Exterior": { "const": "white" } |
| 53 | + }, |
| 54 | + "required": [ "Exterior" ] |
| 55 | + }, |
| 56 | + "then": { |
| 57 | + "properties": { |
| 58 | + "Interior": { "const": "gray" } |
| 59 | + } |
| 60 | + } |
| 61 | + }, |
| 62 | + { |
| 63 | + "if": { |
| 64 | + "properties": { |
| 65 | + "Exterior": { "const": "black" } |
| 66 | + }, |
| 67 | + "required": [ "Exterior" ] |
| 68 | + }, |
| 69 | + "then": { |
| 70 | + "properties": { |
| 71 | + "Interior": { "const": "gray" } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + ] |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +This is fine. But notice how the `then` constraint sets are the same. It'd be better if the conditions could be combined to check if `Exterior` is _either_ `"white"` or `"black"` and just have a single `then`. |
| 80 | + |
| 81 | +## Combining conditions |
| 82 | + |
| 83 | +To do this, we just need to specify the same group in the appropriate `[If]` attributes. |
| 84 | + |
| 85 | +```c# |
| 86 | +[If(nameof(Exterior, "white", "grayRequirement"))] |
| 87 | +[If(nameof(Exterior, "black", "grayRequirement"))] |
| 88 | +class CarColors |
| 89 | +{ |
| 90 | + [Const("gray", ConditionGroup = "grayRequirement")] |
| 91 | + public string Interior { get; set; } |
| 92 | + public string Exterior { get; set; } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +This will collect all of the possible values for that property and render them under an `enum` instead of a `const`. |
| 97 | + |
| 98 | +```json |
| 99 | +{ |
| 100 | + "type": "object", |
| 101 | + "properties": { |
| 102 | + "Interior": { "type": "string" }, |
| 103 | + "Exterior": { "type": "string" } |
| 104 | + }, |
| 105 | + "allOf": [ |
| 106 | + { |
| 107 | + "if": { |
| 108 | + "properties": { |
| 109 | + "Exterior": { "enum": [ "white", "black" ] } |
| 110 | + }, |
| 111 | + "required": [ "Exterior" ] |
| 112 | + }, |
| 113 | + "then": { |
| 114 | + "properties": { |
| 115 | + "Interior": { "const": "gray" } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + ] |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +This is a much more concise schema that better expresses the requirement. |
0 commit comments