Skip to content

Commit b7e31d1

Browse files
committed
add validation for rule_type and rule_units
* ensure valid combinations * require rule_units for all but rule_type: user * require rate_* fields for rule_type: rate
1 parent 664cf9d commit b7e31d1

3 files changed

Lines changed: 195 additions & 20 deletions

File tree

policy/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ An individual `Rule` object is defined by the following fields:
233233
| `rule_id` | UUID | Required | Unique ID of the rule |
234234
| `rule_type` | enum | Required | Type of policy (see [Rule Types](#rule-types)) |
235235
| `geographies` | UUID[] | Required | List of Geography UUIDs (non-overlapping) specifying the covered geography |
236-
| `states` | `{ state: event[] }` | Required | [Vehicle state][vehicle-states] to which this rule applies. Optionally provide a list of specific [vehicle events][#vehicle-events] as a subset of a given status for the rule to apply to. An empty list or `null`/absent defaults to "all". |
237-
| `rule_units` | enum | Required | Measured units of policy (see [Rule Units](#rule-units)) |
236+
| `states` | `{ state: event[] }` | Required | [Vehicle state][vehicle-states] to which this rule applies. Optionally provide a list of specific [vehicle events][#vehicle-events] as a subset of a given status for the rule to apply to. An empty list or `null`/absent defaults to "all". |
237+
| `rule_units` | enum | Conditionally Required | Measured units of policy (see [Rule Units](#rule-units)) |
238238
| `vehicle_types` | `vehicle_type[]` | Optional | Applicable vehicle types, default "all". |
239239
| `propulsion_types` | `propulsion_type[]` | Optional | Applicable vehicle [propulsion types][propulsion-types], default "all". |
240240
| `minimum` | integer | Optional | Minimum value, if applicable (default 0) |
@@ -263,16 +263,18 @@ An individual `Rule` object is defined by the following fields:
263263

264264
### Rule Units
265265

266-
| Name | Rule Types | Description |
267-
| --------- | -------------- | ------------------- |
268-
| `seconds` | `time` | Seconds |
269-
| `minutes` | `time` | Minutes |
270-
| `hours` | `time` | Hours |
271-
| `days` | `time` | Days |
272-
| `mph` | `speed` | Miles per hour |
273-
| `kph` | `speed` | Kilometers per hour |
274-
| `devices` | `count` | Devices |
275-
| `amount` | `rate` | Cost (in [local currency](/general-information.md#costs-and-currencies)) |
266+
| Name | Rule Types | Description |
267+
| --------- | ---------------------- | ------------------- |
268+
| `seconds` | `rate`, `time` | Seconds |
269+
| `minutes` | `rate`, `time` | Minutes |
270+
| `hours` | `rate`, `time` | Hours |
271+
| `days` | `rate`, `time` | Days |
272+
| `amount` | `rate` | Cost (in [local currency](/general-information.md#costs-and-currencies)) |
273+
| `mph` | `speed` | Miles per hour |
274+
| `kph` | `speed` | Kilometers per hour |
275+
| `devices` | `count` | Devices |
276+
277+
[Rule type](#rule-types) `user` has no associated Rule units; `rule_units` is not required when the Rule type is `user`.
276278

277279
[Top][toc]
278280

@@ -296,9 +298,9 @@ Rate recurrences specify when a rate is applied – either once, or periodicall
296298

297299
| Name | Description |
298300
| --------- | ------------------- |
299-
| `once` | Rate is applied once to vehicles entering a matching status from a non-matching status. |
301+
| `once` | Rate is applied once to vehicles entering a matching status from a non-matching status. |
300302
| `each_time_unit` | During each `time_unit`, rate is applied once to vehicles entering or remaining in a matching status. Requires a `time_unit` to be specified using `rule_units`. |
301-
| `per_complete_time_unit` | Rate is applied once per complete `time_unit` that vehicles remain in a matching status. Requires a `time_unit` to be specified using `rule_units`. |
303+
| `per_complete_time_unit` | Rate is applied once per complete `time_unit` that vehicles remain in a matching status. Requires a `time_unit` to be specified using `rule_units`. |
302304

303305
[Top][toc]
304306

policy/policy.json

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@
8686
"rule_id",
8787
"rule_type",
8888
"geographies",
89-
"states",
90-
"rule_units"
89+
"states"
9190
],
9291
"properties": {
9392
"name": {
@@ -233,7 +232,94 @@
233232
"description": "URL to an API endpoint that can provide dynamic information for the measured value",
234233
"format": "uri"
235234
}
236-
}
235+
},
236+
"allOf": [
237+
{
238+
"description": "Valid rule_type and rule_unit values for this rule",
239+
"oneOf": [
240+
{
241+
"properties": {
242+
"rule_type": {
243+
"const": "user"
244+
}
245+
}
246+
},
247+
{
248+
"allOf": [
249+
{
250+
"required": [
251+
"rule_units"
252+
]
253+
},
254+
{
255+
"oneOf": [
256+
{
257+
"properties": {
258+
"rule_type": {
259+
"const": "time"
260+
},
261+
"rule_units": {
262+
"enum": [
263+
"seconds",
264+
"minutes",
265+
"hours",
266+
"days"
267+
]
268+
}
269+
}
270+
},
271+
{
272+
"properties": {
273+
"rule_type": {
274+
"const": "speed"
275+
},
276+
"rule_units": {
277+
"enum": [
278+
"mph",
279+
"kph"
280+
]
281+
}
282+
}
283+
},
284+
{
285+
"properties": {
286+
"rule_type": {
287+
"const": "count"
288+
},
289+
"rule_units": {
290+
"enum": [
291+
"devices"
292+
]
293+
}
294+
}
295+
},
296+
{
297+
"required": [
298+
"rate_amount",
299+
"rate_recurrence"
300+
],
301+
"properties": {
302+
"rule_type": {
303+
"const": "rate"
304+
},
305+
"rule_units": {
306+
"enum": [
307+
"amount",
308+
"seconds",
309+
"minutes",
310+
"hours",
311+
"days"
312+
]
313+
}
314+
}
315+
}
316+
]
317+
}
318+
]
319+
}
320+
]
321+
}
322+
]
237323
},
238324
"currency": {
239325
"$id": "#/definitions/currency",
@@ -325,6 +411,7 @@
325411
"comms_restored",
326412
"compliance_pick_up",
327413
"decommissioned",
414+
"located",
328415
"maintenance",
329416
"maintenance_pick_up",
330417
"missing",

schema/templates/policy/policy.json

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@
8686
"rule_id",
8787
"rule_type",
8888
"geographies",
89-
"states",
90-
"rule_units"
89+
"states"
9190
],
9291
"properties": {
9392
"name": {
@@ -216,7 +215,94 @@
216215
"description": "URL to an API endpoint that can provide dynamic information for the measured value",
217216
"format": "uri"
218217
}
219-
}
218+
},
219+
"allOf": [
220+
{
221+
"description": "Valid rule_type and rule_unit values for this rule",
222+
"oneOf": [
223+
{
224+
"properties": {
225+
"rule_type": {
226+
"const": "user"
227+
}
228+
}
229+
},
230+
{
231+
"allOf": [
232+
{
233+
"required": [
234+
"rule_units"
235+
]
236+
},
237+
{
238+
"oneOf": [
239+
{
240+
"properties": {
241+
"rule_type": {
242+
"const": "time"
243+
},
244+
"rule_units": {
245+
"enum": [
246+
"seconds",
247+
"minutes",
248+
"hours",
249+
"days"
250+
]
251+
}
252+
}
253+
},
254+
{
255+
"properties": {
256+
"rule_type": {
257+
"const": "speed"
258+
},
259+
"rule_units": {
260+
"enum": [
261+
"mph",
262+
"kph"
263+
]
264+
}
265+
}
266+
},
267+
{
268+
"properties": {
269+
"rule_type": {
270+
"const": "count"
271+
},
272+
"rule_units": {
273+
"enum": [
274+
"devices"
275+
]
276+
}
277+
}
278+
},
279+
{
280+
"required": [
281+
"rate_amount",
282+
"rate_recurrence"
283+
],
284+
"properties": {
285+
"rule_type": {
286+
"const": "rate"
287+
},
288+
"rule_units": {
289+
"enum": [
290+
"amount",
291+
"seconds",
292+
"minutes",
293+
"hours",
294+
"days"
295+
]
296+
}
297+
}
298+
}
299+
]
300+
}
301+
]
302+
}
303+
]
304+
}
305+
]
220306
}
221307
},
222308
"required": [

0 commit comments

Comments
 (0)