ondc-code-generator 0.6.22 → 0.7.3
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/.github/copilot-instructions.md +128 -0
- package/GOLANG_IMPLEMENTATION.md +156 -0
- package/README.md +10 -1
- package/dist/bin/cli-tool.d.ts +70 -0
- package/dist/bin/cli-tool.js +310 -0
- package/dist/bin/cli.d.ts +2 -0
- package/dist/bin/cli.js +80 -0
- package/dist/generator/config-compiler.d.ts +1 -0
- package/dist/generator/config-compiler.js +14 -0
- package/dist/generator/generators/go/go-ast.d.ts +1 -0
- package/dist/generator/generators/go/go-ast.js +60 -0
- package/dist/generator/generators/go/go-generator.d.ts +13 -0
- package/dist/generator/generators/go/go-generator.js +322 -0
- package/dist/generator/generators/go/templates/api-tests.mustache +65 -0
- package/dist/generator/generators/go/templates/go-mod.mustache +3 -0
- package/dist/generator/generators/go/templates/index.mustache +34 -0
- package/dist/generator/generators/go/templates/json-normalizer.mustache +155 -0
- package/dist/generator/generators/go/templates/json-path-utils.mustache +63 -0
- package/dist/generator/generators/go/templates/storage-templates/api-save-utils.mustache +84 -0
- package/dist/generator/generators/go/templates/storage-templates/api-save.mustache +44 -0
- package/dist/generator/generators/go/templates/storage-templates/index.mustache +72 -0
- package/dist/generator/generators/go/templates/storage-templates/save-utils.mustache +75 -0
- package/dist/generator/generators/go/templates/storage-templates/storage-interface.mustache +107 -0
- package/dist/generator/generators/go/templates/test-config.mustache +62 -0
- package/dist/generator/generators/go/templates/test-object.mustache +52 -0
- package/dist/generator/generators/go/templates/validation-code.mustache +66 -0
- package/dist/generator/generators/go/templates/validation-utils.mustache +321 -0
- package/dist/generator/generators/typescript/templates/index.mustache +1 -1
- package/dist/generator/generators/typescript/ts-generator.js +2 -2
- package/dist/index.d.ts +7 -1
- package/dist/index.js +6 -1
- package/dist/index.test.js +8 -1
- package/dist/types/build.d.ts +2 -0
- package/dist/types/compiler-types.d.ts +2 -1
- package/dist/types/compiler-types.js +1 -0
- package/dist/utils/fs-utils.d.ts +1 -0
- package/dist/utils/fs-utils.js +24 -0
- package/dist/utils/general-utils/string-utils.d.ts +1 -0
- package/dist/utils/general-utils/string-utils.js +11 -0
- package/package.json +5 -1
- package/sample.md +273 -0
- package/test-python-session.js +0 -0
package/sample.md
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
# JVAL DSL Best Practices & Naming Conventions
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
This document outlines best practices for writing effective JVAL (JSON Validation Language) configurations for the ONDC Code Generator.
|
|
6
|
+
|
|
7
|
+
## Naming Conventions
|
|
8
|
+
|
|
9
|
+
### Test Object Names (`_NAME_`)
|
|
10
|
+
|
|
11
|
+
- **Use snake_case** for consistency with generated function names
|
|
12
|
+
- **Be descriptive and specific** about what the test validates
|
|
13
|
+
- **Include context** when validating nested objects
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
// ✅ Good
|
|
17
|
+
"_NAME_": "validate_context_action_search"
|
|
18
|
+
"_NAME_": "check_provider_locations_present"
|
|
19
|
+
"_NAME_": "verify_payment_settlement_terms"
|
|
20
|
+
|
|
21
|
+
// ❌ Avoid
|
|
22
|
+
"_NAME_": "test1"
|
|
23
|
+
"_NAME_": "checkAction"
|
|
24
|
+
"_NAME_": "validate"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Variable Names
|
|
28
|
+
|
|
29
|
+
- **Use descriptive names** that clearly indicate the data being extracted
|
|
30
|
+
- **Follow snake_case** convention
|
|
31
|
+
- **Include data type hints** when helpful (e.g., `_codes`, `_ids`, `_values`)
|
|
32
|
+
|
|
33
|
+
```json
|
|
34
|
+
// ✅ Good
|
|
35
|
+
"context_action": "$.context.action",
|
|
36
|
+
"provider_location_codes": "$.message.catalog.providers[*].locations[*].id",
|
|
37
|
+
"settlement_terms": "$.message.order.tags[?(@.descriptor.code=='SETTLEMENT_TERMS')].list[*].code"
|
|
38
|
+
|
|
39
|
+
// ❌ Avoid
|
|
40
|
+
"var1": "$.context.action",
|
|
41
|
+
"x": "$.message.catalog.providers[*].locations[*].id",
|
|
42
|
+
"data": "$.some.path"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Test Set Names
|
|
46
|
+
|
|
47
|
+
- **Use lowercase with underscores** for test set keys
|
|
48
|
+
- **Group by API action** or logical functionality
|
|
49
|
+
- **Be specific about the validation scope**
|
|
50
|
+
|
|
51
|
+
```json
|
|
52
|
+
// ✅ Good
|
|
53
|
+
"search_validations": [...],
|
|
54
|
+
"on_search_catalog_validations": [...],
|
|
55
|
+
"payment_settlement_checks": [...]
|
|
56
|
+
|
|
57
|
+
// ❌ Avoid
|
|
58
|
+
"Search": [...],
|
|
59
|
+
"tests": [...],
|
|
60
|
+
"validations": [...]
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## JSONPath Best Practices
|
|
64
|
+
|
|
65
|
+
### Path Structure
|
|
66
|
+
|
|
67
|
+
- **Always start with `$`** for root-level paths
|
|
68
|
+
- **Use specific filters** instead of broad wildcards when possible
|
|
69
|
+
- **Test paths** with sample data before implementation
|
|
70
|
+
|
|
71
|
+
```json
|
|
72
|
+
// ✅ Good - Specific filtering
|
|
73
|
+
"settlement_codes": "$.message.order.tags[?(@.descriptor.code=='SETTLEMENT_TERMS')].list[*].code"
|
|
74
|
+
|
|
75
|
+
// ⚠️ Less optimal - Too broad
|
|
76
|
+
"all_codes": "$.message..code"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Scoping Strategy
|
|
80
|
+
|
|
81
|
+
- **Use `_SCOPE_`** to iterate over arrays of objects
|
|
82
|
+
- **Keep variables relative** to the scope when possible
|
|
83
|
+
- **Avoid deep nesting** in scoped validations
|
|
84
|
+
|
|
85
|
+
```json
|
|
86
|
+
// ✅ Good - Proper scoping
|
|
87
|
+
{
|
|
88
|
+
"_SCOPE_": "$.message.catalog.providers[*]",
|
|
89
|
+
"provider_id": "$.id",
|
|
90
|
+
"location_ids": "$.locations[*].id",
|
|
91
|
+
"_RETURN_": "provider_id are present && location_ids are present"
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ❌ Avoid - Absolute paths in scope
|
|
95
|
+
{
|
|
96
|
+
"_SCOPE_": "$.message.catalog.providers[*]",
|
|
97
|
+
"provider_id": "$.message.catalog.providers[*].id", // Redundant
|
|
98
|
+
"_RETURN_": "provider_id are present"
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## JVAL Expression Guidelines
|
|
103
|
+
|
|
104
|
+
### Readability
|
|
105
|
+
|
|
106
|
+
- **Use parentheses** to clarify complex logic
|
|
107
|
+
- **Break complex expressions** into multiple test objects
|
|
108
|
+
- **Write descriptive variable names** that make expressions self-documenting
|
|
109
|
+
|
|
110
|
+
```json
|
|
111
|
+
// ✅ Good - Clear logic flow
|
|
112
|
+
"_RETURN_": "(context_action are present) && (context_action all in valid_actions)"
|
|
113
|
+
|
|
114
|
+
// ✅ Good - Complex logic broken down
|
|
115
|
+
"_RETURN_": [
|
|
116
|
+
{
|
|
117
|
+
"_NAME_": "check_required_fields",
|
|
118
|
+
"context_action": "$.context.action",
|
|
119
|
+
"message_intent": "$.message.intent",
|
|
120
|
+
"_RETURN_": "context_action are present && message_intent are present"
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
"_NAME_": "validate_action_values",
|
|
124
|
+
"context_action": "$.context.action",
|
|
125
|
+
"valid_actions": ["search", "select", "init"],
|
|
126
|
+
"_RETURN_": "context_action all in valid_actions"
|
|
127
|
+
}
|
|
128
|
+
]
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Error Handling
|
|
132
|
+
|
|
133
|
+
- **Always provide `_DESCRIPTION_`** for meaningful error messages
|
|
134
|
+
- **Use consistent error codes** within logical groups
|
|
135
|
+
- **Make descriptions actionable** for developers
|
|
136
|
+
|
|
137
|
+
```json
|
|
138
|
+
// ✅ Good - Clear, actionable descriptions
|
|
139
|
+
{
|
|
140
|
+
"_NAME_": "validate_context_action_search",
|
|
141
|
+
"context_action": "$.context.action",
|
|
142
|
+
"valid_actions": ["search"],
|
|
143
|
+
"_ERROR_CODE_": 30001,
|
|
144
|
+
"_DESCRIPTION_": "context.action must be 'search' for search API calls",
|
|
145
|
+
"_RETURN_": "context_action all in valid_actions"
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Configuration Structure
|
|
150
|
+
|
|
151
|
+
### Test Organization
|
|
152
|
+
|
|
153
|
+
- **Group related validations** in the same test set
|
|
154
|
+
- **Order tests logically** (basic → complex)
|
|
155
|
+
- **Use consistent error code ranges** per validation group
|
|
156
|
+
|
|
157
|
+
```json
|
|
158
|
+
{
|
|
159
|
+
"x-validations": {
|
|
160
|
+
"_TESTS_": {
|
|
161
|
+
"basic_structure_validations": [
|
|
162
|
+
// Basic field presence checks first
|
|
163
|
+
],
|
|
164
|
+
"business_logic_validations": [
|
|
165
|
+
// Complex business rules second
|
|
166
|
+
],
|
|
167
|
+
"cross_field_validations": [
|
|
168
|
+
// Inter-field dependency checks last
|
|
169
|
+
]
|
|
170
|
+
},
|
|
171
|
+
"_SESSION_DATA_": {
|
|
172
|
+
"search": {
|
|
173
|
+
"transaction_id": "$.context.transaction_id"
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Example: Complete Validation Set
|
|
181
|
+
|
|
182
|
+
```json
|
|
183
|
+
{
|
|
184
|
+
"x-validations": {
|
|
185
|
+
"_TESTS_": {
|
|
186
|
+
"search_api_validations": [
|
|
187
|
+
{
|
|
188
|
+
"_NAME_": "validate_required_context_fields",
|
|
189
|
+
"context_action": "$.context.action",
|
|
190
|
+
"context_domain": "$.context.domain",
|
|
191
|
+
"transaction_id": "$.context.transaction_id",
|
|
192
|
+
"_ERROR_CODE_": 30001,
|
|
193
|
+
"_DESCRIPTION_": "Required context fields must be present in search request",
|
|
194
|
+
"_RETURN_": "context_action are present && context_domain are present && transaction_id are present"
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
"_NAME_": "validate_search_action_value",
|
|
198
|
+
"context_action": "$.context.action",
|
|
199
|
+
"expected_action": ["search"],
|
|
200
|
+
"_ERROR_CODE_": 30002,
|
|
201
|
+
"_DESCRIPTION_": "context.action must be 'search' for search API calls",
|
|
202
|
+
"_RETURN_": "context_action all in expected_action"
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
"_NAME_": "validate_intent_structure",
|
|
206
|
+
"_SCOPE_": "$.message.intent",
|
|
207
|
+
"item_descriptor_name": "$.item.descriptor.name",
|
|
208
|
+
"fulfillment_locations": "$.fulfillment.end.location.area_code",
|
|
209
|
+
"_ERROR_CODE_": 30003,
|
|
210
|
+
"_DESCRIPTION_": "Search intent must contain item descriptor and fulfillment location",
|
|
211
|
+
"_RETURN_": "item_descriptor_name are present || fulfillment_locations are present"
|
|
212
|
+
}
|
|
213
|
+
]
|
|
214
|
+
},
|
|
215
|
+
"_SESSION_DATA_": {
|
|
216
|
+
"search": {
|
|
217
|
+
"transaction_id": "$.context.transaction_id",
|
|
218
|
+
"domain": "$.context.domain"
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## Common Patterns
|
|
226
|
+
|
|
227
|
+
### Enum Validation
|
|
228
|
+
|
|
229
|
+
```json
|
|
230
|
+
{
|
|
231
|
+
"field_value": "$.path.to.field",
|
|
232
|
+
"allowed_values": ["value1", "value2", "value3"],
|
|
233
|
+
"_RETURN_": "field_value all in allowed_values"
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Presence Validation
|
|
238
|
+
|
|
239
|
+
```json
|
|
240
|
+
{
|
|
241
|
+
"required_fields": ["$.field1", "$.field2", "$.field3"],
|
|
242
|
+
"_RETURN_": "required_fields are present"
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Uniqueness Validation
|
|
247
|
+
|
|
248
|
+
```json
|
|
249
|
+
{
|
|
250
|
+
"_SCOPE_": "$.array.path[*]",
|
|
251
|
+
"id_values": "$.id",
|
|
252
|
+
"_RETURN_": "id_values are unique"
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Regex Pattern Validation
|
|
257
|
+
|
|
258
|
+
```json
|
|
259
|
+
{
|
|
260
|
+
"email_field": "$.contact.email",
|
|
261
|
+
"email_pattern": ["^[\\w\\.-]+@[\\w\\.-]+\\.[a-zA-Z]{2,}$"],
|
|
262
|
+
"_RETURN_": "email_field follow regex email_pattern"
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Anti-Patterns to Avoid
|
|
267
|
+
|
|
268
|
+
1. **Generic variable names** like `var1`, `data`, `temp`
|
|
269
|
+
2. **Overly complex single expressions** - break into multiple tests instead
|
|
270
|
+
3. **Missing error descriptions** - always provide meaningful messages
|
|
271
|
+
4. **Inconsistent naming** - stick to snake_case throughout
|
|
272
|
+
5. **Hard-coded values in expressions** - use variables for maintainability
|
|
273
|
+
6. **Too broad JSONPath selectors** - be as specific as possible
|
package/test-python-session.js
DELETED
|
File without changes
|