Skip to content

Commit 7d53fea

Browse files
authored
Merge pull request #3398 from victormlg/CFE-4163-new-strict-option
Added strict option for JSON validation
2 parents afc9cd0 + 9eb7502 commit 7d53fea

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

reference/functions/validdata.markdown

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@ title: validdata
44
published: true
55
---
66

7-
[%CFEngine_function_prototype(data_container, type)%]
7+
[%CFEngine_function_prototype(data_container, type, strict)%]
88

99
**Description:** Validates a JSON container from `data_container` and returns
10-
`true` if the contents are valid JSON.
10+
`"true"` if the contents are valid JSON.
11+
An optional second argument `strict` may be used to enable strict validation.
12+
When set to `"true"` the function will not evaluate to true for JSON primitives.
13+
The default value of `strict`is `"false"`.
14+
15+
The strict behavior matches the expectations in other parts of policy language.
16+
Notably, `data` containers and functions like `readjson()` and `parsejson()` expect JSON containers (arrays and objects) not primitives.
17+
Thus, using `validdata()` with `strict` set to `"true"` is an effective way to check if something can be parsed by those functions.
1118

1219
This function is intended to be expanded with functionality for validating
1320
CSV and YAML files eventually, mirroring `readdata()`. If `type` is `JSON`,
1421
it behaves the same as `validjson()`.
1522

16-
[%CFEngine_function_attributes(data_container, type)%]
23+
[%CFEngine_function_attributes(data_container, type, strict)%]
1724

1825
**Example:**
1926

reference/functions/validjson.markdown

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ title: validjson
44
published: true
55
---
66

7-
[%CFEngine_function_prototype(string)%]
7+
[%CFEngine_function_prototype(string, strict)%]
88

99
**Description:** Validates a JSON container from `string` and returns
10-
`true` if the contents are valid JSON.
10+
`"true"` if the contents are valid JSON.
11+
An optional second argument `strict` may be used to enable strict validation.
12+
When set to `"true"` the function will not evaluate to true for JSON primitives.
13+
The default value of `strict`is `"false"`.
1114

12-
[%CFEngine_function_attributes(string)%]
15+
The strict behavior matches the expectations in other parts of policy language.
16+
Notably, `data` containers and functions like `readjson()` and `parsejson()` expect JSON containers (arrays and objects) not primitives.
17+
Thus, using `validjson()` with `strict` set to `"true"` is an effective way to check if something can be parsed by those functions.
18+
19+
[%CFEngine_function_attributes(string, strict)%]
1320

1421
**Example:**
1522

0 commit comments

Comments
 (0)