mdmodels 0.2.9

A tool to generate models, code and schemas from markdown files
Documentation
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# My model is not validating

This section highlights the most common mistakes that break validation rules. Understanding these pitfalls will help you write valid models and avoid errors during validation and code generation.

## Naming Issues

### ❌ Names Starting with Numbers

**Problem:** Object and attribute names cannot start with a number.

```markdown
### 1Test          <!-- ❌ Invalid: starts with number -->
- 1number: string <!-- ❌ Invalid: starts with number -->
```

**Solution:** Use letters at the beginning of names.

```markdown
### Test1          <!-- ✅ Valid -->
- number1: string <!-- ✅ Valid -->
```

### ❌ Names with Whitespace

**Problem:** Object and attribute names cannot contain spaces.

```markdown
### Test Object    <!-- ❌ Invalid: contains space -->
- some name: string <!-- ❌ Invalid: contains space -->
```

**Solution:** Use underscores or camelCase instead.

```markdown
### TestObject     <!-- ✅ Valid -->
- some_name: string <!-- ✅ Valid -->
- someName: string <!-- ✅ Valid -->
```

### ❌ Names with Special Characters

**Problem:** Only alphanumeric characters and underscores are allowed in names.

```markdown
### User-Profile   <!-- ❌ Invalid: contains hyphen -->
- user.name: string <!-- ❌ Invalid: contains dot -->
```

**Solution:** Use underscores or camelCase.

```markdown
### UserProfile    <!-- ✅ Valid -->
- user_name: string <!-- ✅ Valid -->
```

### Type Definition Issues

#### ❌ Missing Type Definitions

**Problem:** Every attribute must have a type specified.

```markdown
### User
- name            <!-- ❌ Invalid: no type -->
- email: string   <!-- ✅ Valid -->
```

**Solution:** Always specify a type using `- <property>: <TYPE>` syntax.

```markdown
### User
- name: string    <!-- ✅ Valid -->
- email: string   <!-- ✅ Valid -->
```

#### ❌ Undefined Type References

**Problem:** Referenced types must exist in the model or be basic types.

```markdown
### User
- profile: UserProfile <!-- ❌ Invalid if UserProfile doesn't exist -->
- name: string          <!-- ✅ Valid: basic type -->
```

**Solution:** Either define the referenced type or use a basic type (`string`, `number`, `integer`, `boolean`, `float`, `date`, `bytes`).

```markdown
### UserProfile    <!-- Define the type first -->
- bio: string

### User
- profile: UserProfile <!-- ✅ Valid -->
```

#### ❌ Incorrect Type Syntax

**Problem:** Using wrong keywords or syntax for type definitions.

```markdown
### User
- name
  - DType: string <!-- ❌ Invalid: should be "Type" not "DType" -->
```

**Solution:** Use the correct syntax: `- <property>: <TYPE>` or `- Type: <TYPE>`.

```markdown
### User
- name: string    <!-- ✅ Valid: inline syntax -->
- email            <!-- ✅ Valid: block syntax -->
  - Type: string
```

### Duplicate Definitions

#### ❌ Duplicate Object Names

**Problem:** Each object must have a unique name within the model.

```markdown
### User
- name: string

### User  <!-- ❌ Invalid: duplicate name -->
- email: string
```

**Solution:** Rename one of the duplicate objects.

```markdown
### User
- name: string

### UserProfile  <!-- ✅ Valid: unique name -->
- email: string
```

#### ❌ Duplicate Attribute Names

**Problem:** Each attribute within an object must have a unique name.

```markdown
### User
- name: string
- name: string  <!-- ❌ Invalid: duplicate attribute -->
```

**Solution:** Rename one of the duplicate attributes or combine them if they represent the same concept.

```markdown
### User
- first_name: string
- last_name: string  <!-- ✅ Valid: unique names -->
```

#### ❌ Duplicate Enum Names

**Problem:** Each enumeration must have a unique name within the model.

````markdown
### Status

```text
ACTIVE = "active"
```

### Status  <!-- ❌ Invalid: duplicate enum name -->

```text
INACTIVE = "inactive"
```
````

**Solution:** Rename one of the duplicate enumerations.

````markdown
### UserStatus

```text
ACTIVE = "active"
```

### AccountStatus  <!-- ✅ Valid: unique name -->

```text
INACTIVE = "inactive"
```
````

### Structure Issues

#### ❌ Empty Models

**Problem:** Models must contain at least one object definition.

```markdown
# My Model

<!-- ❌ Invalid: no objects defined -->
```

**Solution:** Add at least one object to your model.

```markdown
# My Model

### User
- name: string  <!-- ✅ Valid: model has objects -->
```

#### ❌ Empty Objects

**Problem:** Objects must have at least one attribute (unless `allow_empty: true` is set).

```markdown
### EmptyObject
<!-- ❌ Invalid: no attributes -->
```

**Solution:** Add at least one property or set `allow_empty: true` in frontmatter.

```markdown
---
allow_empty: true
---

### EmptyObject  <!-- ✅ Valid with allow_empty -->
```

Or:

```markdown
### EmptyObject
- id: string  <!-- ✅ Valid: has at least one attribute -->
```

### XML Option Issues

#### ❌ Empty XML Options

**Problem:** XML options cannot be empty or contain only special characters.

```markdown
### User
- name: string
  - XML:        <!-- ❌ Invalid: empty -->
  - XML: @      <!-- ❌ Invalid: only special character -->
```

**Solution:** Provide a valid XML element name or use proper attribute syntax.

```markdown
### User
- name: string
  - XML: userName  <!-- ✅ Valid: element name -->
  - XML: @id       <!-- ✅ Valid: attribute with name -->
```

#### ❌ Special Characters in XML Options

**Problem:** XML element and attribute names cannot contain special characters like colons (unless part of a namespace prefix).

```markdown
### User
- name: string
  - XML: schema:hello  <!-- ❌ Invalid: colon not allowed in element names -->
  - XML: @schema:hello <!-- ❌ Invalid: colon not allowed in attribute names -->
```

**Solution:** Use valid XML names without special characters, or restructure your XML serialization.

```markdown
### User
- name: string
  - XML: hello         <!-- ✅ Valid: simple name -->
  - XML: @hello        <!-- ✅ Valid: attribute -->
```

#### ❌ Invalid XML Wrapped Syntax

**Problem:** XML wrapped options can only contain two levels of nesting.

```markdown
### Test
- items: string[]
  - XML: some/other/path  <!-- ❌ Invalid: more than 2 levels -->
```

**Solution:** Limit XML wrapped paths to two levels, or create intermediate objects for deeper nesting.

```markdown
### Test
- items: string[]
  - XML: items/item  <!-- ✅ Valid: exactly 2 levels -->
```

Or create intermediate objects:

```markdown
### ItemList
- items: Item[]

### Item
- value: string

### Test
- list: ItemList
  - XML: list/items  <!-- ✅ Valid: uses intermediate object -->
```

#### ❌ Invalid Multiple Types with XML

**Problem:** When using multiple types, XML options must match the number of types.

```markdown
### Test
- value: string, float
  - XML: fine, bad:  <!-- ❌ Invalid: second XML option has colon -->
```

**Solution:** Provide valid XML options for each type, matching the type count.

```markdown
### Test
- value: string, float
  - XML: stringValue, floatValue  <!-- ✅ Valid: matches type count -->
```

### Reserved Names

#### ❌ Using Reserved Names

**Problem:** Certain names like `__other__` are reserved and cannot be used as attribute names.

```markdown
### Test
- __other__: string  <!-- ❌ Invalid: reserved name -->
```

**Solution:** Use a different name that doesn't conflict with reserved keywords.

```markdown
### Test
- other_value: string  <!-- ✅ Valid: not reserved -->
```

### Inheritance Issues

#### ❌ Invalid Inheritance Syntax

**Problem:** Inheritance syntax must be properly formatted.

```markdown
### Test [  <!-- ❌ Invalid: incomplete syntax -->
- name: string
```

**Solution:** Use proper inheritance syntax with a valid parent type.

```markdown
### Test [Parent]  <!-- ✅ Valid: proper inheritance -->
- name: string
```

Or if no inheritance:

```markdown
### Test  <!-- ✅ Valid: no inheritance -->
- name: string
```

### Array Type Issues

#### ❌ Mixing Array Syntax Incorrectly

**Problem:** When using multiple types with arrays, the syntax must be consistent.

```markdown
### Test
- primitive: string[], integer, float, boolean  <!-- ❌ Invalid: mixing array and non-array types incorrectly -->
```

**Solution:** Use consistent array syntax or separate into different attributes.

```markdown
### Test
- strings: string[]      <!-- ✅ Valid: array of strings -->
- numbers: integer[]     <!-- ✅ Valid: array of integers -->
- single_value: string   <!-- ✅ Valid: single value -->
```

Or use union types properly:

```markdown
### Test
- values: string | integer | float  <!-- ✅ Valid: union type -->
```

## Quick Reference: Valid vs Invalid

| Issue                   | ❌ Invalid                                       | ✅ Valid                                           |
| ----------------------- | ----------------------------------------------- | ------------------------------------------------- |
| Name starts with number | `1User`, `123test`                              | `User1`, `test123`                                |
| Name with space         | `User Profile`, `user name`                     | `UserProfile`, `user_name`                        |
| Name with special char  | `User-Profile`, `user.name`                     | `UserProfile`, `user_name`                        |
| Missing type            | `- name`                                        | `- name: string`                                  |
| Undefined type          | `- profile: Profile` (if Profile doesn't exist) | `- profile: Profile` (if Profile exists)          |
| Duplicate object        | Two `### User` sections                         | Unique object names                               |
| Duplicate attribute     | Two `- name: string` in same object             | Unique attribute names                            |
| Empty model             | No objects defined                              | At least one object                               |
| Empty object            | `### User` with no attributes                   | `### User` with attributes or `allow_empty: true` |
| Empty XML option        | `- XML:` (empty)                                | `- XML: elementName`                              |
| XML wrapped depth       | `- XML: a/b/c/d` (3+ levels)                    | `- XML: a/b` (2 levels)                           |
| Reserved name           | `- __other__: string`                           | `- other: string`                                 |

## Tips for Avoiding Common Mistakes

1. **Always validate first**: Run `md-models validate -i model.md` before generating code
2. **Start simple**: Begin with basic types and simple structures, then add complexity
3. **Use descriptive names**: Follow naming conventions from the start to avoid refactoring later
4. **Check type references**: Ensure all referenced types exist before using them
5. **Test incrementally**: Add objects and attributes one at a time, validating as you go
6. **Read error messages carefully**: Validation errors include line numbers and specific solutions
7. **Use basic types when possible**: Prefer `string`, `number`, `integer`, `boolean` over custom types when appropriate