Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

IANGAC JSON validator

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
/** @jsxImportSource npm:hono@3/jsx */
import { Hono } from "npm:hono@3";
const app = new Hono();
function analyzeJSON(jsonData) {
const images = jsonData.images;
const allIds = new Set(images.map(img => img.id));
const referencedIds = new Set();
const errors = [];
const warnings = [];
// Check all references and track referenced IDs
images.forEach(image => {
image.related.forEach(ref => {
if (!allIds.has(ref)) {
errors.push(`Error: '${ref}' in '${image.id}' related list doesn't have a matching ID`);
}
referencedIds.add(ref);
});
});
// Check for IDs that are never referenced
allIds.forEach(id => {
if (!referencedIds.has(id)) {
warnings.push(`Warning: '${id}' is never referenced in any related list`);
}
});
return { errors, warnings };
}
app.get("/", (c) =>
c.html(
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" accept=".json,.txt,application/json,text/plain" name="file" />
<button type="submit">Analyze File</button>
</form>,
));
app.post("/", async (c) => {
const formData = await c.req.formData();
const file = formData.get("file");
if (!file || !(file instanceof File)) {
return c.html(<p>No file uploaded or invalid file.</p>);
}
try {
const jsonContent = await file.text();
const jsonData = JSON.parse(jsonContent);
const { errors, warnings } = analyzeJSON(jsonData);
return c.html(
<div>
<h2>Analysis Results</h2>
<h3>Errors:</h3>
<ul>
{errors.map(error => <li>{error}</li>)}
</ul>
<h3>Warnings:</h3>
<ul>
{warnings.map(warning => <li>{warning}</li>)}
</ul>
<a href="/">Analyze Another File</a>
</div>,
);
} catch (error) {
return c.html(<p>Error processing file: {error.message}</p>);
}
});
export default app.fetch;
pfeffunit-iangac_validator.web.val.run
August 29, 2024