@swaggerexpert/arazzo-criterion 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +20 -0
- package/README.md +406 -0
- package/SECURITY.md +15 -0
- package/cjs/apg-lite.cjs +1223 -0
- package/cjs/errors/ArazzoCriterionError.cjs +46 -0
- package/cjs/errors/ArazzoCriterionEvaluateError.cjs +10 -0
- package/cjs/errors/ArazzoCriterionParseError.cjs +10 -0
- package/cjs/evaluate/index.cjs +139 -0
- package/cjs/grammar.cjs +1111 -0
- package/cjs/index.cjs +83 -0
- package/cjs/parse/callbacks/cst.cjs +30 -0
- package/cjs/parse/index.cjs +42 -0
- package/cjs/parse/trace/Expectations.cjs +12 -0
- package/cjs/parse/trace/Trace.cjs +37 -0
- package/cjs/parse/translators/ASTTranslator/index.cjs +17 -0
- package/cjs/parse/translators/ASTTranslator/transformers.cjs +203 -0
- package/cjs/parse/translators/CSTTranslator.cjs +49 -0
- package/cjs/parse/translators/XMLTranslator.cjs +14 -0
- package/cjs/test/index.cjs +20 -0
- package/es/errors/ArazzoCriterionError.mjs +40 -0
- package/es/errors/ArazzoCriterionEvaluateError.mjs +3 -0
- package/es/errors/ArazzoCriterionParseError.mjs +3 -0
- package/es/evaluate/index.mjs +132 -0
- package/es/grammar.mjs +1105 -0
- package/es/index.mjs +11 -0
- package/es/parse/callbacks/cst.mjs +24 -0
- package/es/parse/index.mjs +35 -0
- package/es/parse/trace/Expectations.mjs +6 -0
- package/es/parse/trace/Trace.mjs +30 -0
- package/es/parse/translators/ASTTranslator/index.mjs +9 -0
- package/es/parse/translators/ASTTranslator/transformers.mjs +195 -0
- package/es/parse/translators/CSTTranslator.mjs +42 -0
- package/es/parse/translators/XMLTranslator.mjs +7 -0
- package/es/test/index.mjs +13 -0
- package/package.json +95 -0
- package/types/index.d.ts +283 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
@swaggerexpert/arazzo-criterion
|
|
2
|
+
Copyright 2025, Vladimír Gorej
|
|
3
|
+
@swaggerexpert/arazzo-criterion is licensed under Apache 2.0 license.
|
|
4
|
+
Copy of the Apache 2.0 license can be found in `LICENSE` file.
|
|
5
|
+
|
|
6
|
+
JSONPath: Query Expressions for JSON
|
|
7
|
+
Copyright (c) 2024 IETF Trust and the persons identified as the document authors. All rights reserved.
|
|
8
|
+
https://www.rfc-editor.org/rfc/rfc9535
|
|
9
|
+
The logical / comparison spine of the ABNF grammar (grammar.bnf) - the
|
|
10
|
+
logical-or-expr, logical-and-expr, basic-expr, paren-expr, test-expr,
|
|
11
|
+
comparison-expr, logical-not-op, comparison-op, number, frac, exp, S and B
|
|
12
|
+
rules - is derived from RFC 9535 section 2.3.5.1 and is licensed under the
|
|
13
|
+
Revised BSD License - https://spdx.org/licenses/BSD-3-Clause.html.
|
|
14
|
+
|
|
15
|
+
Arazzo Specification 1.1.0
|
|
16
|
+
Copyright The Linux Foundation
|
|
17
|
+
Fragments of the specification text are embedded in `README.md`.
|
|
18
|
+
The runtime expression rules of the ABNF grammar (grammar.bnf) are inlined from
|
|
19
|
+
the Arazzo Specification (by way of @swaggerexpert/arazzo-runtime-expression).
|
|
20
|
+
Copy of the Apache 2.0 license can be found in `LICENSE` file.
|
package/README.md
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
# @swaggerexpert/arazzo-criterion
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@swaggerexpert/arazzo-criterion)
|
|
4
|
+
[](https://www.npmjs.com/package/@swaggerexpert/arazzo-criterion)
|
|
5
|
+
[](https://github.com/swaggerexpert/arazzo-criterion/actions)
|
|
6
|
+
[](https://dependabot.com/)
|
|
7
|
+
[](https://npm.runkit.com/@swaggerexpert/arazzo-criterion)
|
|
8
|
+
[](https://tidelift.com/subscription/pkg/npm-.swaggerexpert-arazzo-criterion?utm_source=npm-swaggerexpert-arazzo-criterion&utm_medium=referral&utm_campaign=readme)
|
|
9
|
+
|
|
10
|
+
[Arazzo Criterion Objects](https://spec.openapis.org/arazzo/v1.1.0.html#criterion-object) specify the conditions used in `successCriteria` of a [Step Object](https://spec.openapis.org/arazzo/v1.1.0.html#step-object)
|
|
11
|
+
and in the `criteria` of Success and Failure Action Objects.
|
|
12
|
+
|
|
13
|
+
`@swaggerexpert/arazzo-criterion` is a **parser**, **validator** and **evaluator** for the **`simple`** type of Arazzo Criterion conditions **only**.
|
|
14
|
+
The `regex`, `jsonpath` and `xpath` criterion types are **out of scope** — they delegate to external engines (a regular-expression engine, a [JSONPath](https://github.com/swaggerexpert/jsonpath) engine, an XPath engine) and belong in a higher-level evaluator that composes this package with those.
|
|
15
|
+
|
|
16
|
+
The `simple` condition syntax combines literals, comparison and logical operators, property de-reference / index accessors, and [Arazzo Runtime Expressions](https://spec.openapis.org/arazzo/v1.1.0.html#runtime-expressions).
|
|
17
|
+
Runtime Expression operands are parsed by delegating to [@swaggerexpert/arazzo-runtime-expression](https://github.com/swaggerexpert/arazzo-runtime-expression), so their sub-ASTs match that package exactly.
|
|
18
|
+
|
|
19
|
+
In an Arazzo document, a `simple` criterion appears in a step's `successCriteria` (or an action's `criteria`). The `type` defaults to `simple`, so a criterion is usually just a `condition` string:
|
|
20
|
+
|
|
21
|
+
```yaml
|
|
22
|
+
steps:
|
|
23
|
+
- stepId: getPet
|
|
24
|
+
# ...
|
|
25
|
+
successCriteria:
|
|
26
|
+
- condition: $statusCode == 200
|
|
27
|
+
- condition: $response.body.status == 'available' && $response.body.pets[0].id > 0
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Written out as full Criterion Objects, those two entries are:
|
|
31
|
+
|
|
32
|
+
```yaml
|
|
33
|
+
successCriteria:
|
|
34
|
+
- condition: $statusCode == 200
|
|
35
|
+
type: simple # the default; may be omitted
|
|
36
|
+
- condition: $response.body.status == 'available' && $response.body.pets[0].id > 0
|
|
37
|
+
type: simple
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
This library parses, validates and evaluates the `condition` string of such `simple` criteria.
|
|
41
|
+
|
|
42
|
+
It supports the `simple` Criterion Object condition defined in the following Arazzo specification versions:
|
|
43
|
+
|
|
44
|
+
- [Arazzo 1.0.0](https://spec.openapis.org/arazzo/v1.0.0.html)
|
|
45
|
+
- [Arazzo 1.0.1](https://spec.openapis.org/arazzo/v1.0.1.html)
|
|
46
|
+
- [Arazzo 1.1.0](https://spec.openapis.org/arazzo/v1.1.0.html)
|
|
47
|
+
|
|
48
|
+
<table>
|
|
49
|
+
<tr>
|
|
50
|
+
<td align="right" valign="middle">
|
|
51
|
+
<img src="https://raw.githubusercontent.com/swaggerexpert/arazzo-criterion/main/assets/tidelift.webp" alt="Tidelift" width="60" />
|
|
52
|
+
</td>
|
|
53
|
+
<td valign="middle">
|
|
54
|
+
<a href="https://tidelift.com/subscription/pkg/npm-.swaggerexpert-arazzo-criterion?utm_source=npm-swaggerexpert-arazzo-criterion&utm_medium=referral&utm_campaign=readme">
|
|
55
|
+
Get professionally supported @swaggerexpert/arazzo-criterion with Tidelift Subscription.
|
|
56
|
+
</a>
|
|
57
|
+
</td>
|
|
58
|
+
</tr>
|
|
59
|
+
</table>
|
|
60
|
+
|
|
61
|
+
## Table of Contents
|
|
62
|
+
|
|
63
|
+
- [Getting started](#getting-started)
|
|
64
|
+
- [Installation](#installation)
|
|
65
|
+
- [Usage](#usage)
|
|
66
|
+
- [Parsing](#parsing)
|
|
67
|
+
- [Translators](#translators)
|
|
68
|
+
- [Statistics](#statistics)
|
|
69
|
+
- [Tracing](#tracing)
|
|
70
|
+
- [Validation](#validation)
|
|
71
|
+
- [Evaluation](#evaluation)
|
|
72
|
+
- [Errors](#errors)
|
|
73
|
+
- [Grammar](#grammar)
|
|
74
|
+
- [More about the `simple` criterion condition](#more-about-the-simple-criterion-condition)
|
|
75
|
+
- [License](#license)
|
|
76
|
+
|
|
77
|
+
## Getting started
|
|
78
|
+
|
|
79
|
+
### Installation
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
$ npm install @swaggerexpert/arazzo-criterion
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Usage
|
|
86
|
+
|
|
87
|
+
#### Parsing
|
|
88
|
+
|
|
89
|
+
Parsing a criterion condition is as simple as importing the **parse** function and calling it.
|
|
90
|
+
|
|
91
|
+
```js
|
|
92
|
+
import { parse } from '@swaggerexpert/arazzo-criterion';
|
|
93
|
+
|
|
94
|
+
const parseResult = parse('$statusCode == 200 && $response.body.data != null');
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**parseResult** variable has the following shape:
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
{
|
|
101
|
+
result: <ParseResult['result']>,
|
|
102
|
+
tree: <ParseResult['tree']>,
|
|
103
|
+
stats: <ParseResult['stats']>,
|
|
104
|
+
trace: <ParseResult['trace']>,
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
[TypeScript typings](https://github.com/swaggerexpert/arazzo-criterion/blob/main/types/index.d.ts) are available for all fields attached to the parse result object returned by the `parse` function.
|
|
109
|
+
|
|
110
|
+
##### Translators
|
|
111
|
+
|
|
112
|
+
`@swaggerexpert/arazzo-criterion` provides several translators to convert the parse result into different tree representations.
|
|
113
|
+
|
|
114
|
+
###### CST translator
|
|
115
|
+
|
|
116
|
+
[Concrete Syntax Tree](https://en.wikipedia.org/wiki/Parse_tree) (Parse tree) representation is available on the parse result
|
|
117
|
+
when an instance of `CSTTranslator` is provided via the `translator` option to the `parse` function.
|
|
118
|
+
CST is suitable to be consumed by other tools like IDEs, editors, etc...
|
|
119
|
+
|
|
120
|
+
```js
|
|
121
|
+
import { parse, CSTTranslator } from '@swaggerexpert/arazzo-criterion';
|
|
122
|
+
|
|
123
|
+
const { tree: cst } = parse('$statusCode == 200', { translator: new CSTTranslator() });
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
CST tree has a shape documented by [TypeScript typings (CSTNode)](https://github.com/swaggerexpert/arazzo-criterion/blob/main/types/index.d.ts).
|
|
127
|
+
|
|
128
|
+
###### AST translator
|
|
129
|
+
|
|
130
|
+
**Default translator**. [Abstract Syntax Tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree) representation is available on the parse result
|
|
131
|
+
by default or when an instance of `ASTTranslator` is provided via the `translator` option to the `parse` function.
|
|
132
|
+
AST is suitable to be consumed by implementations that need to analyze or evaluate the condition.
|
|
133
|
+
|
|
134
|
+
```js
|
|
135
|
+
import { parse } from '@swaggerexpert/arazzo-criterion';
|
|
136
|
+
|
|
137
|
+
const { tree: ast } = parse('$statusCode == 200 && $response.body.data != null');
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
or
|
|
141
|
+
|
|
142
|
+
```js
|
|
143
|
+
import { parse, ASTTranslator } from '@swaggerexpert/arazzo-criterion';
|
|
144
|
+
|
|
145
|
+
const { tree: ast } = parse('$statusCode == 200', { translator: new ASTTranslator() });
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
AST tree has a shape documented by [TypeScript typings (ConditionAST)](https://github.com/swaggerexpert/arazzo-criterion/blob/main/types/index.d.ts).
|
|
149
|
+
|
|
150
|
+
The AST produced for `$statusCode == 200 && $response.body.data != null` is:
|
|
151
|
+
|
|
152
|
+
```js
|
|
153
|
+
{
|
|
154
|
+
type: 'LogicalExpression',
|
|
155
|
+
operator: '&&',
|
|
156
|
+
left: {
|
|
157
|
+
type: 'BinaryExpression',
|
|
158
|
+
operator: '==',
|
|
159
|
+
left: { type: 'RuntimeExpression', text: '$statusCode', expression: { type: 'StatusCodeExpression' } },
|
|
160
|
+
right: { type: 'Literal', valueType: 'number', value: 200 },
|
|
161
|
+
},
|
|
162
|
+
right: {
|
|
163
|
+
type: 'BinaryExpression',
|
|
164
|
+
operator: '!=',
|
|
165
|
+
left: {
|
|
166
|
+
type: 'RuntimeExpressionNavigation',
|
|
167
|
+
expression: { type: 'RuntimeExpression', text: '$response.body', expression: { ... } },
|
|
168
|
+
navigation: [{ type: 'MemberAccess', name: 'data' }],
|
|
169
|
+
},
|
|
170
|
+
right: { type: 'Literal', valueType: 'null', value: null },
|
|
171
|
+
},
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
A runtime expression with **no** accessors is represented as a `RuntimeExpression` node directly; a `RuntimeExpressionNavigation` node appears only when there is at least one `.member` / `[index]` accessor.
|
|
176
|
+
|
|
177
|
+
###### XML translator
|
|
178
|
+
|
|
179
|
+
An XML string representation of the parse tree is available on the parse result
|
|
180
|
+
when an instance of `XMLTranslator` is provided via the `translator` option to the `parse` function.
|
|
181
|
+
|
|
182
|
+
```js
|
|
183
|
+
import { parse, XMLTranslator } from '@swaggerexpert/arazzo-criterion';
|
|
184
|
+
|
|
185
|
+
const { tree: xml } = parse('$statusCode == 200', { translator: new XMLTranslator() });
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
##### Statistics
|
|
189
|
+
|
|
190
|
+
`parse` returns additional statistical information about the parsing process.
|
|
191
|
+
Collection of the statistics can be enabled by setting the `stats` option to `true`.
|
|
192
|
+
|
|
193
|
+
```js
|
|
194
|
+
import { parse } from '@swaggerexpert/arazzo-criterion';
|
|
195
|
+
|
|
196
|
+
const { stats } = parse('$statusCode == 200', { stats: true });
|
|
197
|
+
|
|
198
|
+
stats.displayStats(); // returns operator statistics as string
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
##### Tracing
|
|
202
|
+
|
|
203
|
+
`parse` returns additional tracing information about the parsing process.
|
|
204
|
+
Tracing can be enabled by setting the `trace` option to `true`. Tracing is essential
|
|
205
|
+
for debugging failed parses or analyzing rule execution flow.
|
|
206
|
+
|
|
207
|
+
```js
|
|
208
|
+
import { parse } from '@swaggerexpert/arazzo-criterion';
|
|
209
|
+
|
|
210
|
+
const { result, trace } = parse('$statusCode <', { trace: true });
|
|
211
|
+
|
|
212
|
+
result.success; // false
|
|
213
|
+
trace.displayTrace(); // returns trace information as string
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Tracing also allows you to infer expected tokens at a failure point. This is useful for generating meaningful syntax error messages.
|
|
217
|
+
|
|
218
|
+
```js
|
|
219
|
+
import { parse } from '@swaggerexpert/arazzo-criterion';
|
|
220
|
+
|
|
221
|
+
const { trace } = parse('$statusCode nonsense', { trace: true });
|
|
222
|
+
|
|
223
|
+
const expectations = trace.inferExpectations();
|
|
224
|
+
console.log(expectations.toString()); // the tokens that could appear at the failure point
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
#### Validation
|
|
228
|
+
|
|
229
|
+
```js
|
|
230
|
+
import { test } from '@swaggerexpert/arazzo-criterion';
|
|
231
|
+
|
|
232
|
+
test('$statusCode == 200'); // => true
|
|
233
|
+
test('$statusCode < 200 < 300'); // => false (chained comparisons are invalid)
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
#### Evaluation
|
|
237
|
+
|
|
238
|
+
`evaluate` runs a condition against caller-supplied values. Because a criterion only becomes concrete once its runtime expressions are resolved against a live context, you provide a `resolve` function. It receives the runtime expression **string** and its parsed **sub-AST**, and returns the concrete value — so you can key on either the raw string or dispatch on the AST `type`.
|
|
239
|
+
|
|
240
|
+
```js
|
|
241
|
+
import { evaluate } from '@swaggerexpert/arazzo-criterion';
|
|
242
|
+
|
|
243
|
+
const context = {
|
|
244
|
+
$statusCode: 200,
|
|
245
|
+
'$response.body': { status: 'Available', data: [{ id: 42 }] },
|
|
246
|
+
};
|
|
247
|
+
const resolve = (expression, ast) => context[expression];
|
|
248
|
+
|
|
249
|
+
evaluate('$statusCode == 200', { resolve }); // => true
|
|
250
|
+
evaluate("$response.body.status == 'available'", { resolve }); // => true (case-insensitive)
|
|
251
|
+
evaluate('$response.body.data[0].id > 10', { resolve }); // => true
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
Evaluation follows the "loose comparison" rules from the Arazzo specification:
|
|
255
|
+
|
|
256
|
+
- string comparisons are **case-insensitive**;
|
|
257
|
+
- numeric strings are **coerced** to numbers when compared with a number;
|
|
258
|
+
- `null` is equal only to `null`; any relational comparison involving `null` is `false`;
|
|
259
|
+
- a condition **passes** when it evaluates to a truthy value and **fails** on `false`, `null`, or a missing value.
|
|
260
|
+
|
|
261
|
+
#### Errors
|
|
262
|
+
|
|
263
|
+
`@swaggerexpert/arazzo-criterion` provides a structured error class hierarchy,
|
|
264
|
+
enabling precise error handling across parsing and evaluation.
|
|
265
|
+
|
|
266
|
+
```js
|
|
267
|
+
import {
|
|
268
|
+
ArazzoCriterionError,
|
|
269
|
+
ArazzoCriterionParseError,
|
|
270
|
+
ArazzoCriterionEvaluateError,
|
|
271
|
+
} from '@swaggerexpert/arazzo-criterion';
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
**ArazzoCriterionError** is the base class for all errors. **ArazzoCriterionParseError** wraps an unexpected error
|
|
275
|
+
raised during parsing, and **ArazzoCriterionEvaluateError** is thrown by `evaluate` (for example, when the condition
|
|
276
|
+
is not valid). Both extend `ArazzoCriterionError` and include the offending `condition`.
|
|
277
|
+
|
|
278
|
+
```js
|
|
279
|
+
import { evaluate, ArazzoCriterionEvaluateError } from '@swaggerexpert/arazzo-criterion';
|
|
280
|
+
|
|
281
|
+
try {
|
|
282
|
+
evaluate('== 200', { resolve: () => undefined }); // invalid condition
|
|
283
|
+
} catch (error) {
|
|
284
|
+
if (error instanceof ArazzoCriterionEvaluateError) {
|
|
285
|
+
console.log(error.condition); // the condition that failed: "== 200"
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
Note: a syntactically invalid condition passed to `parse` does **not** throw - it returns a result with
|
|
291
|
+
`result.success === false` and `tree === undefined`. Use `test` for a simple boolean validity check.
|
|
292
|
+
|
|
293
|
+
#### Grammar
|
|
294
|
+
|
|
295
|
+
New grammar instance can be created in the following way:
|
|
296
|
+
|
|
297
|
+
```js
|
|
298
|
+
import { Grammar } from '@swaggerexpert/arazzo-criterion';
|
|
299
|
+
|
|
300
|
+
const grammar = new Grammar();
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
To obtain the original ABNF grammar as a string:
|
|
304
|
+
|
|
305
|
+
```js
|
|
306
|
+
import { Grammar } from '@swaggerexpert/arazzo-criterion';
|
|
307
|
+
|
|
308
|
+
const grammar = new Grammar();
|
|
309
|
+
|
|
310
|
+
grammar.toString();
|
|
311
|
+
// or
|
|
312
|
+
String(grammar);
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
## More about the `simple` criterion condition
|
|
316
|
+
|
|
317
|
+
The logical / comparison spine follows the structure of [RFC 9535](https://www.rfc-editor.org/rfc/rfc9535) (JSONPath filter expressions):
|
|
318
|
+
a logical layer (`||`, `&&`, `!`, grouping) that composes only booleans, over a flat, non-recursive comparison layer (`comparable OP comparable`).
|
|
319
|
+
This rejects nonsensical forms such as chained comparisons (`a < b < c`) at the grammar level.
|
|
320
|
+
|
|
321
|
+
Operands are Arazzo Runtime Expressions, optionally followed by property-dereference (`.member`) and index (`[n]`) accessors. The grammar matches an
|
|
322
|
+
operand as a single bounded token; the runtime-expression base and the trailing accessors are separated during AST construction by delegating the base
|
|
323
|
+
to [@swaggerexpert/arazzo-runtime-expression](https://github.com/swaggerexpert/arazzo-runtime-expression) (see the grammar header note for why the
|
|
324
|
+
boundary cannot be expressed in a context-free grammar).
|
|
325
|
+
|
|
326
|
+
The `simple` criterion condition is defined by the following [ABNF](https://tools.ietf.org/html/rfc5234) (RFC 5234) syntax:
|
|
327
|
+
|
|
328
|
+
```abnf
|
|
329
|
+
; Arazzo Criterion Object - "simple" condition ABNF syntax
|
|
330
|
+
; https://spec.openapis.org/arazzo/v1.1.0.html#criterion-object
|
|
331
|
+
|
|
332
|
+
; ---------------------------------------------------------------------------
|
|
333
|
+
; Criterion condition (simple type)
|
|
334
|
+
; ---------------------------------------------------------------------------
|
|
335
|
+
|
|
336
|
+
condition = S logical-expr S
|
|
337
|
+
|
|
338
|
+
logical-expr = logical-or-expr
|
|
339
|
+
logical-or-expr = logical-and-expr *( S "||" S logical-and-expr )
|
|
340
|
+
logical-and-expr = basic-expr *( S "&&" S basic-expr )
|
|
341
|
+
|
|
342
|
+
basic-expr = paren-expr / comparison-expr / test-expr
|
|
343
|
+
paren-expr = [ logical-not-op S ] "(" S logical-expr S ")"
|
|
344
|
+
test-expr = [ logical-not-op S ] comparable
|
|
345
|
+
comparison-expr = comparable S comparison-op S comparable
|
|
346
|
+
|
|
347
|
+
logical-not-op = "!"
|
|
348
|
+
comparison-op = "==" / "!=" / "<=" / ">=" / "<" / ">"
|
|
349
|
+
|
|
350
|
+
; ---------------------------------------------------------------------------
|
|
351
|
+
; Comparables: literals or operands (runtime expression + navigation, matched
|
|
352
|
+
; as one bounded token)
|
|
353
|
+
; ---------------------------------------------------------------------------
|
|
354
|
+
|
|
355
|
+
comparable = literal / runtime-expression-operand
|
|
356
|
+
runtime-expression-operand = "$" 1*operand-char
|
|
357
|
+
operand-char = %x22-25 / %x27 / %x2A-3B / %x3F-5A / %x5B-5D / %x5E-7A / %x7E / %x80-10FFFF
|
|
358
|
+
|
|
359
|
+
; Navigation over the resolved runtime-expression value (secondary entry point;
|
|
360
|
+
; parsed from the operand remainder during AST construction).
|
|
361
|
+
runtime-expression-navigation = 1*( member-access / index-access )
|
|
362
|
+
member-access = "." member-name
|
|
363
|
+
index-access = "[" index "]"
|
|
364
|
+
member-name = 1*( ALPHA / DIGIT / "_" / "-" )
|
|
365
|
+
index = 1*DIGIT
|
|
366
|
+
|
|
367
|
+
; ---------------------------------------------------------------------------
|
|
368
|
+
; Literals
|
|
369
|
+
; ---------------------------------------------------------------------------
|
|
370
|
+
|
|
371
|
+
literal = number / string / boolean / null
|
|
372
|
+
boolean = "true" / "false"
|
|
373
|
+
null = "null"
|
|
374
|
+
|
|
375
|
+
number = ( int / "-0" ) [ frac ] [ exp ]
|
|
376
|
+
int = "0" / ( [ "-" ] DIGIT1 *DIGIT )
|
|
377
|
+
frac = "." 1*DIGIT
|
|
378
|
+
exp = ( "e" / "E" ) [ "-" / "+" ] 1*DIGIT
|
|
379
|
+
DIGIT1 = %x31-39 ; 1-9 non-zero digit
|
|
380
|
+
|
|
381
|
+
; single-quoted; a literal quote is escaped by doubling it ('')
|
|
382
|
+
string = squote *( escaped-quote / string-char ) squote
|
|
383
|
+
escaped-quote = squote squote ; '' represents a single '
|
|
384
|
+
string-char = %x20-26 / %x28-10FFFF ; any char except squote (%x27)
|
|
385
|
+
squote = %x27 ; '
|
|
386
|
+
|
|
387
|
+
; ---------------------------------------------------------------------------
|
|
388
|
+
; Whitespace (optional blank space), per RFC 9535
|
|
389
|
+
; ---------------------------------------------------------------------------
|
|
390
|
+
|
|
391
|
+
S = *B
|
|
392
|
+
B = %x20 / %x09 / %x0A / %x0D ; space, tab, LF, CR
|
|
393
|
+
|
|
394
|
+
; ---------------------------------------------------------------------------
|
|
395
|
+
; Core rules (RFC 5234 B.1)
|
|
396
|
+
; ---------------------------------------------------------------------------
|
|
397
|
+
|
|
398
|
+
ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
|
|
399
|
+
DIGIT = %x30-39 ; 0-9
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
## License
|
|
403
|
+
|
|
404
|
+
`@swaggerexpert/arazzo-criterion` is licensed under [Apache 2.0 license](https://github.com/swaggerexpert/arazzo-criterion/blob/main/LICENSE).
|
|
405
|
+
`@swaggerexpert/arazzo-criterion` comes with an explicit [NOTICE](https://github.com/swaggerexpert/arazzo-criterion/blob/main/NOTICE) file
|
|
406
|
+
containing additional legal notices and information.
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
If you believe you've found an exploitable security issue in @swaggerexpert/arazzo-criterion, please don't create a public issue.
|
|
4
|
+
|
|
5
|
+
## Supported Versions
|
|
6
|
+
|
|
7
|
+
| Version | Supported |
|
|
8
|
+
|---------|--------------------|
|
|
9
|
+
| ^1.0.0 | :white_check_mark: |
|
|
10
|
+
|
|
11
|
+
## Reporting a Vulnerability
|
|
12
|
+
|
|
13
|
+
To report a vulnerability please send an email with the details to contact@swaggerexpert.com.
|
|
14
|
+
|
|
15
|
+
I'll acknowledge receipt of your report ASAP, and set expectations on how I plan to handle it.
|