check-rule-mate 0.5.6 → 0.5.9

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.
@@ -1,617 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * check-rule-mate — Auto Documentation Generator
4
- */
5
-
6
- // import fs from "fs";
7
- // import path from "path";
8
- // import process from "process";
9
-
10
- const fs = require('fs');
11
- const path = require('path');
12
- const process = require('process');
13
- let esbuild;
14
- try {
15
- esbuild = require('esbuild');
16
- } catch {
17
- console.warn(
18
- '[check-rule-mate] esbuild not found. ' +
19
- 'Install it to enable playground generation.'
20
- );
21
- process.exit(1);
22
- }
23
-
24
- /* ---------------------------------------
25
- * CLI ARGS
26
- * -------------------------------------*/
27
- const args = process.argv.slice(2);
28
- const getArg = (flag) => {
29
- const index = args.indexOf(flag);
30
- return index !== -1 ? args[index + 1] : null;
31
- };
32
-
33
- const RULES_DIR = getArg('--rules');
34
- const SCHEMAS_DIR = getArg('--schemas');
35
- const ERRORS_DIR = getArg('--errors')
36
- const OPTIONS_DIR = getArg('--options');
37
- const OUTPUT = getArg('--out') || "check-rule-mate-docs.html";
38
- const OUTPUT_DIR = path.dirname(OUTPUT);
39
-
40
- const OPTIONS = OPTIONS_DIR ? JSON.parse(fs.readFileSync(OPTIONS_DIR, "utf-8")) : null;
41
-
42
-
43
- if (OPTIONS) {
44
- let entryPointFile = Object.keys(OPTIONS.validators).map((key) => `var ${key} = require('${OPTIONS.validators[key]}')`).join('\n');
45
- entryPointFile += `\n window.validatorHelpers = {${Object.keys(OPTIONS.validators).map((key) => `${key}: ${key}`)}}`
46
-
47
- fs.writeFileSync(`./check-rule-mate.validators.docs.js`, entryPointFile);
48
-
49
- esbuild.build({
50
- entryPoints: ['./node_modules/check-rule-mate/src/main.js'],
51
- bundle: true,
52
- outfile: `${OUTPUT_DIR}/check-rule-mate.js`,
53
- platform: 'browser',
54
- target: 'node16',
55
- format: 'iife',
56
- minify: false
57
- }).then(() => {
58
- console.log('Build completed successfully: check-rule-mate.js');
59
-
60
- const checkRuleMateFIle = fs.readFileSync(`${OUTPUT_DIR}/check-rule-mate.js`, 'utf-8');
61
- const lines = checkRuleMateFIle.split('\n')
62
- lines.shift();
63
- lines.splice(lines.length - 2, 1)
64
- fs.writeFileSync(`${OUTPUT_DIR}/check-rule-mate.js`, lines.join('\n'))
65
- }).catch(() => process.exit(1));
66
-
67
- esbuild.build({
68
- entryPoints: [`./check-rule-mate.validators.docs.js`],
69
- bundle: true,
70
- outfile: `${OUTPUT_DIR}/index.js`,
71
- platform: 'browser',
72
- target: 'node16',
73
- format: 'iife',
74
- minify: false
75
- }).then(() => {
76
- console.log('Build completed successfully: index.js');
77
- fs.rmSync('./check-rule-mate.validators.docs.js')
78
- }).catch(() => process.exit(1));
79
- }
80
-
81
-
82
- if (!RULES_DIR || !SCHEMAS_DIR) {
83
- console.error(`
84
- Usage:
85
- npx check-rule-mate-auto-docs --rules ./rules --schemas ./schemas --errors ./errors --out docs.html
86
- `);
87
- process.exit(1);
88
- }
89
-
90
- /* ---------------------------------------
91
- * HELPERS
92
- * -------------------------------------*/
93
- const readJSONFiles = (dir) => {
94
- return fs.readdirSync(dir)
95
- .filter(f => f.endsWith(".json"))
96
- .map(file => ({
97
- name: file.replace(".json", ""),
98
- content: JSON.parse(fs.readFileSync(path.join(dir, file), "utf-8"))
99
- }));
100
- };
101
-
102
- const rulesFiles = readJSONFiles(RULES_DIR);
103
- const schemasFiles = readJSONFiles(SCHEMAS_DIR);
104
- const errorsFiles = readJSONFiles(ERRORS_DIR);
105
-
106
-
107
- /* ---------------------------------------
108
- * INDEX RELATIONSHIPS
109
- * -------------------------------------*/
110
- const ruleUsageMap = {};
111
- const errorUsageMap = {}
112
-
113
- schemasFiles.forEach(schemaFile => {
114
- Object.entries(schemaFile.content).forEach(([field, config]) => {
115
- const ruleName = config.rule.split("--")[0];
116
- if (!ruleUsageMap[ruleName]) ruleUsageMap[ruleName] = [];
117
- ruleUsageMap[ruleName].push({
118
- schema: schemaFile.name,
119
- field,
120
- rule: config.rule
121
- });
122
- });
123
- });
124
-
125
- rulesFiles.forEach(rulesFile => {
126
- Object.entries(rulesFile.content).forEach(([field, config]) => {
127
- Object.entries(config.error).forEach(([key, value]) => {
128
- const errorName = value;
129
- if (!errorUsageMap[errorName]) errorUsageMap[errorName] = [];
130
- errorUsageMap[errorName].push({
131
- file: rulesFile.name,
132
- field,
133
- error: value
134
- });
135
- });
136
- if (config?.modifier) {
137
- Object.entries(config.modifier).forEach(([modifierKey, modifier]) => {
138
- if (modifier?.error) {
139
- Object.entries(modifier.error).forEach(([key, value]) => {
140
- const errorName = value;
141
- if (!errorUsageMap[errorName]) errorUsageMap[errorName] = [];
142
- errorUsageMap[errorName].push({
143
- file: rulesFile.name,
144
- field,
145
- error: value
146
- });
147
- });
148
- }
149
- });
150
- }
151
- });
152
- });
153
-
154
-
155
- /* ---------------------------------------
156
- * HTML GENERATION
157
- * -------------------------------------*/
158
- const html = `
159
- <!DOCTYPE html>
160
- <html lang="en">
161
- <head>
162
- <meta charset="UTF-8" />
163
- <title>check-rule-mate — Documentation</title>
164
- <style>
165
- ${generateCSS()}
166
- </style>
167
- ${OPTIONS ? `
168
- <script src="./check-rule-mate.js" defer></script>
169
- <script src="./index.js" defer></script>`
170
- : ''}
171
- </head>
172
- <body>
173
-
174
- <aside class="sidebar">
175
- <input id="search" placeholder="Search..." />
176
-
177
- <h3>Schemas</h3>
178
- ${schemasFiles.map(s => `
179
- <a href="#schema-${s.name}">${s.name}</a>
180
- `).join("")}
181
-
182
- <h3>Rules</h3>
183
- ${rulesFiles.map(rf =>
184
- Object.keys(rf.content).map(rule => `
185
- <a href="#rule-${rule}">${rf.name} - ${rule}</a>
186
- `).join("")
187
- ).join("")}
188
-
189
- <h3>Errors</h3>
190
- ${errorsFiles.map(errorFile =>
191
- Object.keys(errorFile.content).map(error => `
192
- <a href="#error-${error}">${errorFile.name} - ${error}</a>
193
- `).join("")
194
- ).join("")}
195
- </aside>
196
-
197
- <main>
198
- <section>
199
- <h1>check-rule-mate</h1>
200
- <p class="muted">
201
- Visual documentation of validation rules and schemas.
202
- </p>
203
- </section>
204
-
205
- ${renderSchemas(schemasFiles)}
206
- ${renderRules(rulesFiles, ruleUsageMap)}
207
- ${renderErrors(errorsFiles, errorUsageMap)}
208
- </main>
209
-
210
- <script>
211
- ${generateClientJS()}
212
- </script>
213
-
214
- </body>
215
- </html>
216
- `;
217
-
218
- fs.writeFileSync(OUTPUT, html);
219
- console.log(`✔ Documentation generated at ${OUTPUT}`);
220
-
221
- /* ---------------------------------------
222
- * RENDERERS
223
- * -------------------------------------*/
224
- function renderSchemas(schemas) {
225
- return schemas.map(schema => `
226
- <section id="schema-${schema.name}" class="card">
227
- <h2>Schema: ${schema.name}</h2>
228
-
229
- <table>
230
- <thead>
231
- <tr>
232
- <th>Field</th>
233
- <th>Rule</th>
234
- <th class="text-center">Required</th>
235
- <th class="text-center">Cache</th>
236
- </tr>
237
- </thead>
238
- <tbody>
239
- ${Object.entries(schema.content).map(([field, cfg]) => `
240
- <tr>
241
- <td>${field}</td>
242
- <td>
243
- <a href="#rule-${cfg.rule.split("--")[0]}">
244
- ${cfg.rule}
245
- </a>
246
- </td>
247
- <td class="text-center">${cfg.required ? "✔" : "optional"}</td>
248
- <td class="text-center">${cfg.cache === false ? "off" : "✔"}</td>
249
- </tr>
250
- `).join("")}
251
- </tbody>
252
- </table>
253
- ${OPTIONS ? `
254
- <details class="schema-test">
255
- <summary>Schema Test (Experimental)</summary>
256
- <div class="schema-test-container">
257
- <form id="schema:${schema.name}" data-form="schema--${schema.name}">
258
- ${Object.entries(schema.content).map(([field, cfg]) => {
259
- const attributesHTML = cfg.attributesHTML ? Object.keys(cfg.attributesHTML).map((key) => `${key}="${cfg?.attributesHTML[key]}"`).join(' ') : null
260
- return `
261
- <div style="display: flex; flex-direction: column;">
262
- <label for="${field}">${field} (${cfg.rule})</label>
263
- <input name="${field}" ${cfg.required ? 'required': ''} ${attributesHTML}/>
264
- </div>
265
- `}).join("")}
266
- <div style="display: flex; flex-direction: column;">
267
- <label for="check-rule-mate-validators">VALIDATORS</label>
268
- <input name="check-rule-mate-validators" placeholder="Add the validator object/function/class name - example: myValidator" required/>
269
- </div>
270
- <div style="display: flex; flex-direction: column;">
271
- <label for="check-rule-mate-rules">RULES</label>
272
- <input name="check-rule-mate-rules" placeholder="Add the rule file name - example: myRules" required/>
273
- </div>
274
- <button type="submit">Validate Schema</button>
275
- </form>
276
- <div class="schema-test-result">
277
- <span>Result:</span>
278
- <textarea id="textarea-schema-${schema.name}"></textarea>
279
- </div>
280
- </div>
281
- </details>
282
- ` : ''}
283
- </section>
284
- `).join("");
285
- }
286
-
287
- function renderRules(rulesFiles, usageMap) {
288
- return rulesFiles.map(file =>
289
- Object.entries(file.content).map(([ruleName, rule]) => `
290
- <section id="rule-${ruleName}" class="card">
291
- <h2>Rule: ${ruleName} (${file.name})</h2>
292
- ${rule?.docs?.description ?
293
- `<p>${rule.docs.description}</p>`
294
- : ''}
295
-
296
- <h3>Validation Flow</h3>
297
- <div class="flow">
298
- ${rule.validate.map(v => `
299
- <div class="flow-step">${v}</div>
300
- <div class="flow-arrow">→</div>
301
- `).join("")}
302
- <div class="flow-step success">valid</div>
303
- </div>
304
-
305
- <h3>Error Codes</h3>
306
- ${renderRulesErrors(rule.error)}
307
-
308
- ${rule.modifier ? `
309
- <h3>Modifiers</h3>
310
- ${Object.entries(rule.modifier).map(([mod, modRule]) => `
311
- <div class="modifier">
312
- <span class="tag modifier">${mod}</span>
313
- ${modRule?.docs?.description ? `<p>${modRule.docs.description}</p>` : ''}
314
-
315
- <div class="flow">
316
- ${modRule.validate.map(v => `
317
- <div class="flow-step">${v}</div>
318
- <div class="flow-arrow">→</div>
319
- `).join("")}
320
- <div class="flow-step success">valid</div>
321
- </div>
322
-
323
- <h4>Error Codes</h4>
324
- ${renderRulesErrors(modRule.error)}
325
- </div>
326
- `).join("")}
327
- ` : ""}
328
-
329
- <h3>Used by Schemas</h3>
330
- <ul>
331
- ${(usageMap[ruleName] || []).map(u =>
332
- `<li>${u.schema} → <strong>${u.field}</strong> (${u.rule})</li>`
333
- ).join("") || "<li>Not used</li>"}
334
- </ul>
335
-
336
- ${rule?.docs?.notes ?
337
- `
338
- <h3>Notes</h3>
339
- <div class="rule-notes">${rule?.docs?.notes}</div>
340
- `
341
- : ''}
342
- </section>
343
- `).join("")
344
- ).join("");
345
- }
346
-
347
- function renderRulesErrors(errors = {}) {
348
- return `
349
- <ul>
350
- ${Object.entries(errors).map(([k, v]) =>
351
- `<li><a href="#error-${v.split('.')[0]}" class="tag error">${k}</a> ${v}</li>`
352
- ).join("")}
353
- </ul>
354
- `;
355
- }
356
-
357
- function renderErrors(errorFiles, usageMap) {
358
- return errorFiles.map(file =>
359
- Object.entries(file.content).map(([errorName, errors]) => `
360
- <section id="error-${errorName}" class="card">
361
- <h2>Error: ${errorName} (${file.name})</h2>
362
- <ul>
363
- ${Object.entries(errors).map(([key, value]) => `
364
- <li><span class="tag error">${key}</span> ${value} </li>
365
- `).join('')}
366
- </ul>
367
-
368
- <h3>Used by Rules</h3>
369
- <ul>
370
- ${Object.entries(errors).map(([key, value]) => `
371
- ${(usageMap[`${errorName}.${key}`] || []).map(u =>
372
- `<li>${u.file} → <strong>${u.field}</strong> (${u.error})</li>`
373
- ).join("") || "<li>Not used</li>"}
374
- `).join('')}
375
- </ul>
376
- </section>
377
- `).join('')).join('');
378
- }
379
-
380
- /* ---------------------------------------
381
- * CSS
382
- * -------------------------------------*/
383
- function generateCSS() {
384
- return `
385
- body {
386
- margin: 0;
387
- font-family: Inter, system-ui, sans-serif;
388
- display: grid;
389
- grid-template-columns: 280px 1fr;
390
- background: #0f172a;
391
- color: #e5e7eb;
392
- }
393
-
394
- .sidebar {
395
- padding: 16px;
396
- background: #020617;
397
- overflow-y: auto;
398
- }
399
-
400
- .sidebar input {
401
- width: 100%;
402
- padding: 8px;
403
- border-radius: 6px;
404
- border: none;
405
- margin-bottom: 16px;
406
- }
407
-
408
- .sidebar a {
409
- display: block;
410
- padding: 6px 10px;
411
- color: #e5e7eb;
412
- text-decoration: none;
413
- border-radius: 6px;
414
- }
415
-
416
- .sidebar a:hover {
417
- background: rgba(56,189,248,0.2);
418
- }
419
-
420
- a {
421
- color: #e5e7eb;
422
- }
423
-
424
- main {
425
- padding: 32px;
426
- overflow-y: auto;
427
- }
428
-
429
- .card {
430
- background: #020617;
431
- border-radius: 12px;
432
- padding: 24px;
433
- margin-bottom: 32px;
434
- border: 1px solid rgba(255,255,255,0.05);
435
- }
436
-
437
- .flow {
438
- display: flex;
439
- align-items: center;
440
- gap: 12px;
441
- flex-wrap: wrap;
442
- }
443
-
444
- .flow-step {
445
- padding: 10px 14px;
446
- border-radius: 8px;
447
- background: rgba(56,189,248,0.15);
448
- }
449
-
450
- .flow-arrow {
451
- opacity: 0.5;
452
- }
453
-
454
- .success {
455
- background: rgba(34,197,94,0.2);
456
- }
457
-
458
- .tag {
459
- padding: 4px 8px;
460
- border-radius: 999px;
461
- font-size: 14px;
462
- }
463
-
464
- .tag.error {
465
- display: inline-block;
466
- margin-bottom: 8px;
467
- background: rgba(239,68,68,0.2);
468
- text-decoration: none;
469
- }
470
-
471
- .tag.modifier {
472
- display: inline-block;
473
- font-size: 16px;
474
- margin-bottom: 8px;
475
- font-weight: 600;
476
- background: rgba(167,139,250,0.2);
477
- }
478
-
479
- table {
480
- width: 100%;
481
- border-collapse: collapse;
482
- }
483
-
484
- td, th {
485
- padding: 8px;
486
- border-bottom: 1px solid rgba(255,255,255,0.05);
487
- }
488
-
489
- th {
490
- text-align: left;
491
- border-bottom: 1px solid #f4f4f4;
492
- }
493
-
494
- .rule-notes {
495
- padding: 16px;
496
- color: black;
497
- background: #f4f4f4;
498
- border-radius: 12px;
499
- }
500
-
501
- .text-center {
502
- text-align: center;
503
- }
504
-
505
- .schema-test {
506
- margin-top: 32px;
507
- }
508
-
509
- .schema-test-container {
510
- display: flex;
511
- width: 100%;
512
- margin: 32px 0 24px 0;
513
- gap: 40px;
514
- }
515
-
516
- .schema-test-container > * {
517
- width: 100%;
518
- }
519
-
520
- .schema-test-result span {
521
- display: block;
522
- font-size: 20px;
523
- font-weight: 600;
524
- margin-bottom: 16px;
525
- }
526
-
527
- .schema-test-result textarea {
528
- width: 100%;
529
- height: 80%;
530
- color: #f4f4f4;
531
- background-color: #111111;
532
- border: none;
533
- }
534
-
535
- button {
536
- cursor: pointer;
537
- }
538
-
539
- form button[type="submit"] {
540
- width: 100%;
541
- padding: 16px 24px;
542
- min-width: 120px;
543
- min-height: 48px;
544
- margin-top: 16px;
545
- font-size: 16px;
546
- font-weight: 600;
547
- color: #444444;
548
- background-color: #eede91;
549
- }
550
-
551
- label {
552
- margin-bottom: 4px;
553
- font-size: 16px;
554
- color: #e5e7eb;
555
- }
556
-
557
- .schema-test input {
558
- min-height: 28px;
559
- padding: 4px 8px 4px 8px;
560
- margin-bottom: 16px;
561
- font-size: 16px;
562
- color: #e5e7eb;
563
- background-color: transparent;
564
- border: none;
565
- border-bottom: 1px solid white;
566
- }
567
- `;
568
- }
569
-
570
- /* ---------------------------------------
571
- * CLIENT JS
572
- * -------------------------------------*/
573
- function generateClientJS() {
574
- return `
575
- const search = document.getElementById("search");
576
- search.addEventListener("input", e => {
577
- const value = e.target.value.toLowerCase();
578
- document.querySelectorAll("section.card").forEach(card => {
579
- card.style.display = card.innerText.toLowerCase().includes(value)
580
- ? ""
581
- : "none";
582
- });
583
- });
584
-
585
- ${OPTIONS ? `
586
- setTimeout(() => {
587
- const rulesFiles = ${JSON.stringify(rulesFiles)}
588
- const schemasFiles = ${JSON.stringify(schemasFiles)}
589
-
590
- const formElements = document.querySelectorAll('form[data-form]');
591
- formElements.forEach((formElement) => {
592
- formElement.addEventListener('submit', async (e) => {
593
- e.preventDefault();
594
- const inputElements = formElement.querySelectorAll('input');
595
- const formName = formElement.dataset.form.split('--')[1];
596
- const formData = {};
597
- inputElements.forEach((inputElement) => {
598
- formData[inputElement.name] = inputElement.value;
599
- })
600
-
601
- const validatorHelper = window.validatorHelpers[formData["check-rule-mate-validators"]][formData["check-rule-mate-validators"]];
602
-
603
- const rules = rulesFiles.filter((rulesFile) => rulesFile.name === formData["check-rule-mate-rules"])[0].content;
604
- const schema = schemasFiles.filter((schemaFile) => schemaFile.name === formName)[0].content;
605
-
606
- const validator = createValidator(formData, {validationHelpers: validatorHelper, rules: rules, schema: schema, options: { propertiesMustMatch: false, abortEarly: false, cache: true}});
607
- const result = await validator.validate();
608
- console.log(formName, result);
609
-
610
- const textAreaElement = document.querySelector('#textarea-schema-'+formName);
611
- textAreaElement.value = JSON.stringify(result);
612
- });
613
- });
614
- }, 1000);
615
- `: ''}
616
- `;
617
- }