| 1 |
Parsing comma separated items (CommaSeparated): |
| 2 |
docs: scalar/comma-separated |
| 3 |
based on: strictyaml |
| 4 |
description: | |
| 5 |
Comma-separated values can be validated and parsed |
| 6 |
using the CommaSeparated validator. |
| 7 |
|
| 8 |
Note that the space following the commas is stripped by |
| 9 |
default when parsed. |
| 10 |
given: |
| 11 |
setup: | |
| 12 |
from strictyaml import CommaSeparated, Int, Str, Map, load, as_document |
| 13 |
from ensure import Ensure |
| 14 |
|
| 15 |
int_schema = Map({"a": CommaSeparated(Int())}) |
| 16 |
|
| 17 |
str_schema = Map({"a": CommaSeparated(Str())}) |
| 18 |
variations: |
| 19 |
Parse as int: |
| 20 |
given: |
| 21 |
yaml_snippet: | |
| 22 |
a: 1, 2, 3 |
| 23 |
steps: |
| 24 |
- Run: |
| 25 |
code: | |
| 26 |
Ensure(load(yaml_snippet, int_schema)).equals({"a": [1, 2, 3]}) |
| 27 |
|
| 28 |
Parse as string: |
| 29 |
given: |
| 30 |
yaml_snippet: | |
| 31 |
a: 1, 2, 3 |
| 32 |
steps: |
| 33 |
- Run: |
| 34 |
code: | |
| 35 |
Ensure(load(yaml_snippet, str_schema)).equals({"a": ["1", "2", "3"]}) |
| 36 |
|
| 37 |
Parse empty comma separated string: |
| 38 |
given: |
| 39 |
yaml_snippet: | |
| 40 |
a: |
| 41 |
steps: |
| 42 |
- Run: |
| 43 |
code: | |
| 44 |
Ensure(load(yaml_snippet, str_schema)).equals({"a": []}) |
| 45 |
|
| 46 |
Invalid int comma separated sequence: |
| 47 |
given: |
| 48 |
yaml_snippet: | |
| 49 |
a: 1, x, 3 |
| 50 |
steps: |
| 51 |
- Run: |
| 52 |
code: load(yaml_snippet, int_schema) |
| 53 |
raises: |
| 54 |
type: strictyaml.exceptions.YAMLValidationError |
| 55 |
message: |- |
| 56 |
when expecting an integer |
| 57 |
in "<unicode string>", line 2, column 1: |
| 58 |
|
| 59 |
^ (line: 2) |
| 60 |
found arbitrary text |
| 61 |
in "<unicode string>", line 1, column 1: |
| 62 |
a: 1, x, 3 |
| 63 |
^ (line: 1) |
| 64 |
|
| 65 |
Serialize list to comma separated sequence: |
| 66 |
steps: |
| 67 |
- Run: |
| 68 |
code: | |
| 69 |
print(as_document({"a": [1, 2, 3]}, int_schema).as_yaml()) |
| 70 |
will output: 'a: 1, 2, 3' |
| 71 |
|
| 72 |
Serialize valid string to comma separated sequence: |
| 73 |
steps: |
| 74 |
- Run: |
| 75 |
code: | |
| 76 |
print(as_document({"a": "1,2,3"}, int_schema).as_yaml()) |
| 77 |
will output: 'a: 1,2,3' |
| 78 |
|
| 79 |
Serialize empty list to comma separated sequence: |
| 80 |
steps: |
| 81 |
- Run: |
| 82 |
code: | |
| 83 |
print(as_document({"a": []}, int_schema).as_yaml()) |
| 84 |
will output: 'a:' |
| 85 |
|
| 86 |
Serialize invalid string to comma separated sequence: |
| 87 |
steps: |
| 88 |
- Run: |
| 89 |
code: | |
| 90 |
print(as_document({"a": "1,x,3"}, int_schema).as_yaml()) |
| 91 |
raises: |
| 92 |
type: strictyaml.exceptions.YAMLSerializationError |
| 93 |
message: "'x' not an integer." |
| 94 |
|
| 95 |
Attempt to serialize neither list nor string raises exception: |
| 96 |
steps: |
| 97 |
- Run: |
| 98 |
code: | |
| 99 |
as_document({"a": 1}, int_schema) |
| 100 |
raises: |
| 101 |
type: strictyaml.exceptions.YAMLSerializationError |
| 102 |
message: expected string or list, got '1' of type 'int' |