@dmitryvim/form-builder 0.1.4 → 0.1.6

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/docs/schema.md ADDED
@@ -0,0 +1,427 @@
1
+ # Schema Reference
2
+
3
+ Complete reference for Form Builder JSON Schema v0.3 format.
4
+
5
+ ## Schema Structure
6
+
7
+ ```typescript
8
+ interface FormSchema {
9
+ version: "0.3"; // Required: Schema version
10
+ title: string; // Form title
11
+ elements: FormElement[]; // Array of form elements
12
+ }
13
+ ```
14
+
15
+ ## Field Types
16
+
17
+ ### Text Field
18
+
19
+ Single line text input with validation.
20
+
21
+ ```json
22
+ {
23
+ "type": "text",
24
+ "key": "name",
25
+ "label": "Full Name",
26
+ "required": true,
27
+ "minLength": 2,
28
+ "maxLength": 50,
29
+ "pattern": "^[A-Za-z ]+$",
30
+ "default": ""
31
+ }
32
+ ```
33
+
34
+ **Properties:**
35
+ - `minLength` - Minimum character count
36
+ - `maxLength` - Maximum character count
37
+ - `pattern` - Regular expression for validation
38
+ - `default` - Default value
39
+
40
+ ### Textarea Field
41
+
42
+ Multi-line text input.
43
+
44
+ ```json
45
+ {
46
+ "type": "textarea",
47
+ "key": "description",
48
+ "label": "Description",
49
+ "required": false,
50
+ "minLength": 0,
51
+ "maxLength": 1000,
52
+ "default": ""
53
+ }
54
+ ```
55
+
56
+ **Properties:** Same as text field
57
+
58
+ ### Number Field
59
+
60
+ Numeric input with constraints.
61
+
62
+ ```json
63
+ {
64
+ "type": "number",
65
+ "key": "age",
66
+ "label": "Age",
67
+ "required": true,
68
+ "min": 0,
69
+ "max": 120,
70
+ "decimals": 0,
71
+ "step": 1,
72
+ "default": 0
73
+ }
74
+ ```
75
+
76
+ **Properties:**
77
+ - `min` - Minimum value
78
+ - `max` - Maximum value
79
+ - `decimals` - Number of decimal places (0-8)
80
+ - `step` - Step increment for input
81
+
82
+ ### Select Field
83
+
84
+ Dropdown selection with predefined options.
85
+
86
+ ```json
87
+ {
88
+ "type": "select",
89
+ "key": "country",
90
+ "label": "Country",
91
+ "required": true,
92
+ "options": [
93
+ { "value": "us", "label": "United States" },
94
+ { "value": "ca", "label": "Canada" },
95
+ { "value": "uk", "label": "United Kingdom" }
96
+ ],
97
+ "default": "us"
98
+ }
99
+ ```
100
+
101
+ **Properties:**
102
+ - `options` - Array of value/label pairs
103
+
104
+ ### File Field
105
+
106
+ Single file upload with preview.
107
+
108
+ ```json
109
+ {
110
+ "type": "file",
111
+ "key": "avatar",
112
+ "label": "Profile Picture",
113
+ "required": true,
114
+ "accept": {
115
+ "extensions": ["jpg", "jpeg", "png"],
116
+ "mime": ["image/jpeg", "image/png"]
117
+ },
118
+ "maxSizeMB": 5
119
+ }
120
+ ```
121
+
122
+ **Properties:**
123
+ - `accept.extensions` - Allowed file extensions
124
+ - `accept.mime` - Allowed MIME types
125
+ - `maxSizeMB` - Maximum file size in megabytes
126
+
127
+ ### Files Field
128
+
129
+ Multiple file upload with count limits.
130
+
131
+ ```json
132
+ {
133
+ "type": "files",
134
+ "key": "documents",
135
+ "label": "Documents",
136
+ "required": false,
137
+ "accept": {
138
+ "extensions": ["pdf", "doc", "docx"],
139
+ "mime": ["application/pdf"]
140
+ },
141
+ "minCount": 0,
142
+ "maxCount": 5,
143
+ "maxSizeMB": 10
144
+ }
145
+ ```
146
+
147
+ **Properties:**
148
+ - `minCount` - Minimum number of files
149
+ - `maxCount` - Maximum number of files
150
+ - All `file` type properties
151
+
152
+ ### Group Field
153
+
154
+ Nested object structure with optional repetition.
155
+
156
+ ```json
157
+ {
158
+ "type": "group",
159
+ "key": "address",
160
+ "label": "Address",
161
+ "elements": [
162
+ {
163
+ "type": "text",
164
+ "key": "street",
165
+ "label": "Street Address",
166
+ "required": true
167
+ },
168
+ {
169
+ "type": "text",
170
+ "key": "city",
171
+ "label": "City",
172
+ "required": true
173
+ }
174
+ ]
175
+ }
176
+ ```
177
+
178
+ **With Repetition and Custom Element Titles:**
179
+ ```json
180
+ {
181
+ "type": "group",
182
+ "key": "slides",
183
+ "label": "Video Slides",
184
+ "element_label": "Slide $index",
185
+ "repeat": {
186
+ "min": 1,
187
+ "max": 5
188
+ },
189
+ "elements": [
190
+ {
191
+ "type": "file",
192
+ "key": "image",
193
+ "label": "Slide Image",
194
+ "required": true
195
+ },
196
+ {
197
+ "type": "text",
198
+ "key": "title",
199
+ "label": "Slide Title",
200
+ "required": true
201
+ }
202
+ ]
203
+ }
204
+ ```
205
+
206
+ **Properties:**
207
+ - `elements` - Array of nested form elements
208
+ - `element_label` - Optional custom title for each group item (supports `$index` placeholder)
209
+ - `repeat.min` - Minimum number of repetitions
210
+ - `repeat.max` - Maximum number of repetitions
211
+
212
+ ## Common Properties
213
+
214
+ All field types support these properties:
215
+
216
+ ```typescript
217
+ interface BaseElement {
218
+ type: string; // Field type
219
+ key: string; // Unique identifier
220
+ label: string; // Display label
221
+ required?: boolean; // Required validation (default: false)
222
+ default?: any; // Default value (not allowed for file types)
223
+ }
224
+ ```
225
+
226
+ ## Complete Example
227
+
228
+ Based on the sample from `docs/13_form_builder.html`:
229
+
230
+ ```json
231
+ {
232
+ "version": "0.3",
233
+ "title": "Asset Uploader with Slides",
234
+ "elements": [
235
+ {
236
+ "type": "file",
237
+ "key": "cover",
238
+ "label": "Cover image",
239
+ "required": true,
240
+ "accept": {
241
+ "extensions": ["png", "jpg", "jpeg"],
242
+ "mime": ["image/png", "image/jpeg"]
243
+ },
244
+ "maxSizeMB": 25
245
+ },
246
+ {
247
+ "type": "files",
248
+ "key": "assets",
249
+ "label": "Additional images",
250
+ "required": false,
251
+ "accept": {
252
+ "extensions": ["png", "jpg"],
253
+ "mime": ["image/png", "image/jpeg"]
254
+ },
255
+ "minCount": 0,
256
+ "maxCount": 10,
257
+ "maxSizeMB": 25
258
+ },
259
+ {
260
+ "type": "text",
261
+ "key": "title",
262
+ "label": "Project title",
263
+ "required": true,
264
+ "minLength": 1,
265
+ "maxLength": 120,
266
+ "pattern": "^[A-Za-z0-9 _-]+$",
267
+ "default": "My Project"
268
+ },
269
+ {
270
+ "type": "textarea",
271
+ "key": "description",
272
+ "label": "Description",
273
+ "required": false,
274
+ "minLength": 0,
275
+ "maxLength": 2000,
276
+ "default": ""
277
+ },
278
+ {
279
+ "type": "select",
280
+ "key": "theme",
281
+ "label": "Theme",
282
+ "required": true,
283
+ "options": [
284
+ { "value": "light", "label": "Light" },
285
+ { "value": "dark", "label": "Dark" }
286
+ ],
287
+ "default": "dark"
288
+ },
289
+ {
290
+ "type": "number",
291
+ "key": "opacity",
292
+ "label": "Opacity",
293
+ "required": true,
294
+ "min": 0,
295
+ "max": 1,
296
+ "decimals": 2,
297
+ "step": 0.01,
298
+ "default": 0.85
299
+ },
300
+ {
301
+ "type": "group",
302
+ "key": "slides",
303
+ "label": "Slides",
304
+ "repeat": {
305
+ "min": 1,
306
+ "max": 5
307
+ },
308
+ "elements": [
309
+ {
310
+ "type": "text",
311
+ "key": "title",
312
+ "label": "Slide title",
313
+ "required": true,
314
+ "minLength": 1,
315
+ "maxLength": 80,
316
+ "default": ""
317
+ },
318
+ {
319
+ "type": "textarea",
320
+ "key": "body",
321
+ "label": "Slide text",
322
+ "required": true,
323
+ "minLength": 1,
324
+ "maxLength": 1000,
325
+ "default": ""
326
+ }
327
+ ]
328
+ }
329
+ ]
330
+ }
331
+ ```
332
+
333
+ ## Output Format
334
+
335
+ Form submission produces structured JSON data:
336
+
337
+ ```json
338
+ {
339
+ "cover": "res_abc123def456",
340
+ "assets": [
341
+ "res_def456ghi789",
342
+ "res_ghi789jkl012"
343
+ ],
344
+ "title": "My Amazing Project",
345
+ "description": "A detailed description of the project...",
346
+ "theme": "dark",
347
+ "opacity": 0.85,
348
+ "slides": [
349
+ {
350
+ "title": "Introduction",
351
+ "body": "Welcome to our presentation..."
352
+ },
353
+ {
354
+ "title": "Features",
355
+ "body": "Here are the key features..."
356
+ }
357
+ ]
358
+ }
359
+ ```
360
+
361
+ **Key Points:**
362
+ - File fields return resource IDs (e.g., `res_abc123def456`)
363
+ - Files fields return arrays of resource IDs
364
+ - Group fields return objects or arrays of objects
365
+ - All other fields return their literal values
366
+
367
+ ## Validation Rules
368
+
369
+ ### Schema Validation
370
+
371
+ The form builder validates schemas before rendering:
372
+
373
+ - `version` must be `"0.3"`
374
+ - `elements` must be an array
375
+ - Each element must have `type` and `key`
376
+ - No duplicate keys within the same scope
377
+ - File fields cannot have `default` values
378
+ - Select fields' `default` must be in `options`
379
+ - Numeric constraints must be logical (min ≤ max)
380
+
381
+ ### Field Validation
382
+
383
+ During form interaction:
384
+
385
+ - **Required fields**: Must have values
386
+ - **Text/Textarea**: Length constraints and pattern matching
387
+ - **Number**: Range constraints and decimal places
388
+ - **File/Files**: Type restrictions, size limits, count limits
389
+ - **Select**: Value must be in options list
390
+
391
+ ### Custom Validation
392
+
393
+ For advanced validation, handle the `formSubmit` event:
394
+
395
+ ```javascript
396
+ window.addEventListener('message', (event) => {
397
+ if (event.data.type === 'formSubmit') {
398
+ const { data, schema } = event.data;
399
+
400
+ // Custom validation logic
401
+ if (data.email && !isValidEmail(data.email)) {
402
+ // Handle validation error
403
+ return;
404
+ }
405
+
406
+ // Process valid submission
407
+ handleFormSubmission(data);
408
+ }
409
+ });
410
+ ```
411
+
412
+ ## Schema Evolution
413
+
414
+ ### Version History
415
+
416
+ - **v0.3**: Current version with file upload support
417
+ - Future versions will maintain backwards compatibility within the same major version
418
+
419
+ ### Migration Guidelines
420
+
421
+ When updating schemas:
422
+ 1. Always specify the correct version
423
+ 2. Test with the form builder before deployment
424
+ 3. Use schema validation to catch errors early
425
+ 4. Provide migration paths for breaking changes
426
+
427
+ This schema reference provides complete documentation for creating effective forms with the Form Builder library.
package/package.json CHANGED
@@ -3,15 +3,18 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.1.4",
6
+ "version": "0.1.6",
7
7
  "description": "A reusable JSON schema form builder library",
8
8
  "main": "dist/form-builder.js",
9
9
  "files": [
10
- "dist"
10
+ "dist",
11
+ "docs"
11
12
  ],
12
13
  "scripts": {
13
- "build": "cp public/index.html dist/index.html",
14
+ "build": "mkdir -p dist && cp public/index.html dist/sample.html",
15
+ "build:lib": "echo 'Library already built at dist/form-builder.js'",
14
16
  "dev": "serve public",
17
+ "dev:sample": "serve dist --single",
15
18
  "test": "jest",
16
19
  "format": "prettier --write ."
17
20
  },