-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.cov
More file actions
154 lines (122 loc) · 2.65 KB
/
Copy pathvalidation.cov
File metadata and controls
154 lines (122 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// JSON Validation Example
// Demonstrates JSON validation and type checking
// ============================================================
// External JSON Functions (from std.json)
// ============================================================
snippet id="json.is_valid" kind="extern-abstract"
signature
fn name="is_valid"
param name="input" type="String"
returns type="Bool"
end
end
metadata
description="Check if string is valid JSON without parsing. Returns true if valid."
cost_hint=cheap
end
end
snippet id="json.parse" kind="extern-abstract"
signature
fn name="parse"
param name="input" type="String"
returns union
type="Json"
type="JsonError"
end
end
end
metadata
description="Parse JSON string into Json type. Returns JsonError on invalid syntax."
cost_hint=moderate
end
end
snippet id="json.stringify" kind="extern-abstract"
signature
fn name="stringify"
param name="value" type="Json"
returns type="String"
end
end
metadata
description="Serialize Json value to compact JSON string"
cost_hint=cheap
end
end
// ============================================================
// Example Functions
// ============================================================
snippet id="examples.json_validate" kind="fn"
effects
effect console
end
signature
fn name="validate_and_process"
param name="input" type="String"
returns type="Unit"
end
end
body
// First check if valid JSON
step id="s1" kind="call"
fn="json.is_valid"
arg name="input" from="input"
as="is_valid"
end
step id="s2" kind="if"
condition="is_valid"
then
step id="s2a" kind="call"
fn="console.println"
arg name="message" lit="Valid JSON"
as="_"
end
end
else
step id="s2b" kind="call"
fn="console.error"
arg name="message" lit="Invalid JSON syntax"
as="_"
end
end
as="_"
end
step id="s3" kind="return"
variant type="Unit"
end
as="_"
end
end
end
snippet id="examples.json_round_trip" kind="fn"
signature
fn name="round_trip_test"
param name="json_str" type="String"
returns type="Bool"
end
end
body
// Parse
step id="s1" kind="call"
fn="json.parse"
arg name="input" from="json_str"
as="parsed"
end
// Stringify
step id="s2" kind="call"
fn="json.stringify"
arg name="value" from="parsed"
as="stringified"
end
// Parse again
step id="s3" kind="call"
fn="json.parse"
arg name="input" from="stringified"
as="reparsed"
end
// Both parse operations should succeed for valid JSON
step id="s4" kind="return"
lit=true
as="_"
end
end
end