@datenlotse/jsonjoy-builder 0.4.0
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/LICENSE +21 -0
- package/README.md +203 -0
- package/dist/components/SchemaEditor/AddFieldButton.js +312 -0
- package/dist/components/SchemaEditor/JsonSchemaEditor.js +166 -0
- package/dist/components/SchemaEditor/JsonSchemaVisualizer.js +96 -0
- package/dist/components/SchemaEditor/SchemaField.js +104 -0
- package/dist/components/SchemaEditor/SchemaFieldList.js +84 -0
- package/dist/components/SchemaEditor/SchemaPropertyEditor.js +249 -0
- package/dist/components/SchemaEditor/SchemaTypeSelector.js +55 -0
- package/dist/components/SchemaEditor/SchemaVisualEditor.js +77 -0
- package/dist/components/SchemaEditor/TypeDropdown.js +72 -0
- package/dist/components/SchemaEditor/TypeEditor.js +71 -0
- package/dist/components/SchemaEditor/types/ArrayEditor.js +173 -0
- package/dist/components/SchemaEditor/types/BooleanEditor.js +107 -0
- package/dist/components/SchemaEditor/types/NumberEditor.js +583 -0
- package/dist/components/SchemaEditor/types/ObjectEditor.js +90 -0
- package/dist/components/SchemaEditor/types/StringEditor.js +542 -0
- package/dist/components/features/JsonValidator.js +239 -0
- package/dist/components/features/SchemaInferencer.js +107 -0
- package/dist/components/ui/badge.js +25 -0
- package/dist/components/ui/button.js +41 -0
- package/dist/components/ui/dialog.js +73 -0
- package/dist/components/ui/input.js +11 -0
- package/dist/components/ui/label.js +13 -0
- package/dist/components/ui/select.js +90 -0
- package/dist/components/ui/switch.js +14 -0
- package/dist/components/ui/tabs.js +24 -0
- package/dist/components/ui/tooltip.js +15 -0
- package/dist/hooks/use-monaco-theme.js +197 -0
- package/dist/hooks/use-translation.js +14 -0
- package/dist/i18n/locales/de.js +143 -0
- package/dist/i18n/locales/en.js +143 -0
- package/dist/i18n/locales/es.js +143 -0
- package/dist/i18n/locales/fr.js +143 -0
- package/dist/i18n/locales/ru.js +143 -0
- package/dist/i18n/locales/uk.js +143 -0
- package/dist/i18n/locales/zh.js +143 -0
- package/dist/i18n/translation-context.js +4 -0
- package/dist/i18n/translation-keys.js +0 -0
- package/dist/index.css +3830 -0
- package/dist/index.d.ts +995 -0
- package/dist/index.js +10 -0
- package/dist/lib/schema-inference.js +266 -0
- package/dist/lib/schemaCompile.js +113 -0
- package/dist/lib/schemaEditor.js +167 -0
- package/dist/lib/utils.js +40 -0
- package/dist/types/jsonSchema.js +98 -0
- package/dist/types/validation.js +215 -0
- package/dist/utils/jsonValidator.js +162 -0
- package/package.json +112 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
const simpleTypes = [
|
|
3
|
+
"string",
|
|
4
|
+
"number",
|
|
5
|
+
"integer",
|
|
6
|
+
"boolean",
|
|
7
|
+
"object",
|
|
8
|
+
"array",
|
|
9
|
+
"null"
|
|
10
|
+
];
|
|
11
|
+
const baseSchema = z.object({
|
|
12
|
+
$id: z.string().optional(),
|
|
13
|
+
$schema: z.string().optional(),
|
|
14
|
+
$ref: z.string().optional(),
|
|
15
|
+
$anchor: z.string().optional(),
|
|
16
|
+
$dynamicRef: z.string().optional(),
|
|
17
|
+
$dynamicAnchor: z.string().optional(),
|
|
18
|
+
$vocabulary: z.record(z.string(), z.boolean()).optional(),
|
|
19
|
+
$comment: z.string().optional(),
|
|
20
|
+
title: z.string().optional(),
|
|
21
|
+
description: z.string().optional(),
|
|
22
|
+
default: z.unknown().optional(),
|
|
23
|
+
deprecated: z.boolean().optional(),
|
|
24
|
+
readOnly: z.boolean().optional(),
|
|
25
|
+
writeOnly: z.boolean().optional(),
|
|
26
|
+
examples: z.array(z.unknown()).optional(),
|
|
27
|
+
type: z.union([
|
|
28
|
+
z["enum"](simpleTypes),
|
|
29
|
+
z.array(z["enum"](simpleTypes))
|
|
30
|
+
]).optional(),
|
|
31
|
+
minLength: z.number().int().min(0).optional(),
|
|
32
|
+
maxLength: z.number().int().min(0).optional(),
|
|
33
|
+
pattern: z.string().optional(),
|
|
34
|
+
format: z.string().optional(),
|
|
35
|
+
contentMediaType: z.string().optional(),
|
|
36
|
+
contentEncoding: z.string().optional(),
|
|
37
|
+
multipleOf: z.number().positive().optional(),
|
|
38
|
+
minimum: z.number().optional(),
|
|
39
|
+
maximum: z.number().optional(),
|
|
40
|
+
exclusiveMinimum: z.number().optional(),
|
|
41
|
+
exclusiveMaximum: z.number().optional(),
|
|
42
|
+
minItems: z.number().int().min(0).optional(),
|
|
43
|
+
maxItems: z.number().int().min(0).optional(),
|
|
44
|
+
uniqueItems: z.boolean().optional(),
|
|
45
|
+
minContains: z.number().int().min(0).optional(),
|
|
46
|
+
maxContains: z.number().int().min(0).optional(),
|
|
47
|
+
required: z.array(z.string()).optional(),
|
|
48
|
+
minProperties: z.number().int().min(0).optional(),
|
|
49
|
+
maxProperties: z.number().int().min(0).optional(),
|
|
50
|
+
dependentRequired: z.record(z.string(), z.array(z.string())).optional(),
|
|
51
|
+
const: z.unknown().optional(),
|
|
52
|
+
enum: z.array(z.unknown()).optional()
|
|
53
|
+
});
|
|
54
|
+
const jsonSchemaType = z.lazy(()=>z.union([
|
|
55
|
+
baseSchema.extend({
|
|
56
|
+
$defs: z.record(z.string(), jsonSchemaType).optional(),
|
|
57
|
+
contentSchema: jsonSchemaType.optional(),
|
|
58
|
+
items: jsonSchemaType.optional(),
|
|
59
|
+
prefixItems: z.array(jsonSchemaType).optional(),
|
|
60
|
+
contains: jsonSchemaType.optional(),
|
|
61
|
+
unevaluatedItems: jsonSchemaType.optional(),
|
|
62
|
+
properties: z.record(z.string(), jsonSchemaType).optional(),
|
|
63
|
+
patternProperties: z.record(z.string(), jsonSchemaType).optional(),
|
|
64
|
+
additionalProperties: z.union([
|
|
65
|
+
jsonSchemaType,
|
|
66
|
+
z.boolean()
|
|
67
|
+
]).optional(),
|
|
68
|
+
propertyNames: jsonSchemaType.optional(),
|
|
69
|
+
dependentSchemas: z.record(z.string(), jsonSchemaType).optional(),
|
|
70
|
+
unevaluatedProperties: jsonSchemaType.optional(),
|
|
71
|
+
allOf: z.array(jsonSchemaType).optional(),
|
|
72
|
+
anyOf: z.array(jsonSchemaType).optional(),
|
|
73
|
+
oneOf: z.array(jsonSchemaType).optional(),
|
|
74
|
+
not: jsonSchemaType.optional(),
|
|
75
|
+
if: jsonSchemaType.optional(),
|
|
76
|
+
then: jsonSchemaType.optional(),
|
|
77
|
+
else: jsonSchemaType.optional()
|
|
78
|
+
}),
|
|
79
|
+
z.boolean()
|
|
80
|
+
]));
|
|
81
|
+
function isBooleanSchema(schema) {
|
|
82
|
+
return "boolean" == typeof schema;
|
|
83
|
+
}
|
|
84
|
+
function isObjectSchema(schema) {
|
|
85
|
+
return !isBooleanSchema(schema);
|
|
86
|
+
}
|
|
87
|
+
function asObjectSchema(schema) {
|
|
88
|
+
return isObjectSchema(schema) ? schema : {
|
|
89
|
+
type: "null"
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function getSchemaDescription(schema) {
|
|
93
|
+
return isObjectSchema(schema) ? schema.description || "" : "";
|
|
94
|
+
}
|
|
95
|
+
function withObjectSchema(schema, fn, defaultValue) {
|
|
96
|
+
return isObjectSchema(schema) ? fn(schema) : defaultValue;
|
|
97
|
+
}
|
|
98
|
+
export { asObjectSchema, baseSchema, getSchemaDescription, isBooleanSchema, isObjectSchema, jsonSchemaType, withObjectSchema };
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import zod from "zod";
|
|
2
|
+
import { baseSchema } from "./jsonSchema.js";
|
|
3
|
+
function refineRangeConsistency(min, isMinExclusive, max, isMaxExclusive) {
|
|
4
|
+
if (void 0 !== min && void 0 !== max && min > max) return false;
|
|
5
|
+
if (isMinExclusive && isMaxExclusive && max - min < 2) return false;
|
|
6
|
+
if ((isMinExclusive || isMaxExclusive) && max - min < 1) return false;
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
const getJsonStringType = (t)=>zod.object({
|
|
10
|
+
minLength: zod.number().int({
|
|
11
|
+
message: t.typeValidationErrorIntValue
|
|
12
|
+
}).min(0, {
|
|
13
|
+
message: t.typeValidationErrorNegativeLength
|
|
14
|
+
}).optional(),
|
|
15
|
+
maxLength: zod.number().int({
|
|
16
|
+
message: t.typeValidationErrorIntValue
|
|
17
|
+
}).min(0, {
|
|
18
|
+
message: t.typeValidationErrorNegativeLength
|
|
19
|
+
}).optional(),
|
|
20
|
+
pattern: baseSchema.shape.pattern,
|
|
21
|
+
format: baseSchema.shape.format,
|
|
22
|
+
enum: baseSchema.shape["enum"],
|
|
23
|
+
contentMediaType: baseSchema.shape.contentMediaType,
|
|
24
|
+
contentEncoding: baseSchema.shape.contentEncoding
|
|
25
|
+
}).refine(({ minLength, maxLength })=>refineRangeConsistency(minLength, false, maxLength, false), {
|
|
26
|
+
message: t.stringValidationErrorLengthRange,
|
|
27
|
+
path: [
|
|
28
|
+
"length"
|
|
29
|
+
]
|
|
30
|
+
});
|
|
31
|
+
const getJsonNumberType = (t)=>zod.object({
|
|
32
|
+
multipleOf: zod.number().positive({
|
|
33
|
+
message: t.typeValidationErrorPositive
|
|
34
|
+
}).optional(),
|
|
35
|
+
minimum: baseSchema.shape.minimum,
|
|
36
|
+
maximum: baseSchema.shape.maximum,
|
|
37
|
+
exclusiveMinimum: baseSchema.shape.exclusiveMinimum,
|
|
38
|
+
exclusiveMaximum: baseSchema.shape.exclusiveMaximum,
|
|
39
|
+
enum: baseSchema.shape["enum"]
|
|
40
|
+
}).refine(({ minimum, exclusiveMinimum, maximum, exclusiveMaximum })=>refineRangeConsistency(minimum, false, maximum, false) && refineRangeConsistency(minimum, false, exclusiveMaximum, true) && refineRangeConsistency(exclusiveMinimum, true, maximum, false) && refineRangeConsistency(exclusiveMinimum, true, exclusiveMaximum, true), {
|
|
41
|
+
message: t.numberValidationErrorMinMax,
|
|
42
|
+
path: [
|
|
43
|
+
"minMax"
|
|
44
|
+
]
|
|
45
|
+
}).refine(({ minimum, exclusiveMinimum })=>void 0 === exclusiveMinimum || void 0 === minimum, {
|
|
46
|
+
message: t.numberValidationErrorBothExclusiveAndInclusiveMin,
|
|
47
|
+
path: [
|
|
48
|
+
"redundantMinimum"
|
|
49
|
+
]
|
|
50
|
+
}).refine(({ maximum, exclusiveMaximum })=>void 0 === exclusiveMaximum || void 0 === maximum, {
|
|
51
|
+
message: t.numberValidationErrorBothExclusiveAndInclusiveMax,
|
|
52
|
+
path: [
|
|
53
|
+
"redundantMaximum"
|
|
54
|
+
]
|
|
55
|
+
}).refine(({ enum: enumValues, minimum, maximum, exclusiveMinimum, exclusiveMaximum })=>{
|
|
56
|
+
if (!enumValues || 0 === enumValues.length) return true;
|
|
57
|
+
return enumValues.every((val)=>{
|
|
58
|
+
if ("number" != typeof val) return false;
|
|
59
|
+
if (void 0 !== minimum && val < minimum) return false;
|
|
60
|
+
if (void 0 !== maximum && val > maximum) return false;
|
|
61
|
+
if (void 0 !== exclusiveMinimum && val <= exclusiveMinimum) return false;
|
|
62
|
+
if (void 0 !== exclusiveMaximum && val >= exclusiveMaximum) return false;
|
|
63
|
+
return true;
|
|
64
|
+
});
|
|
65
|
+
}, {
|
|
66
|
+
message: t.numberValidationErrorEnumOutOfRange,
|
|
67
|
+
path: [
|
|
68
|
+
"enum"
|
|
69
|
+
]
|
|
70
|
+
});
|
|
71
|
+
const getJsonArrayType = (t)=>zod.object({
|
|
72
|
+
minItems: zod.number().int({
|
|
73
|
+
message: t.typeValidationErrorIntValue
|
|
74
|
+
}).min(0, {
|
|
75
|
+
message: t.typeValidationErrorNegativeLength
|
|
76
|
+
}).optional(),
|
|
77
|
+
maxItems: zod.number().int({
|
|
78
|
+
message: t.typeValidationErrorIntValue
|
|
79
|
+
}).min(0, {
|
|
80
|
+
message: t.typeValidationErrorNegativeLength
|
|
81
|
+
}).optional(),
|
|
82
|
+
uniqueItems: zod.boolean().optional(),
|
|
83
|
+
minContains: zod.number().int({
|
|
84
|
+
message: t.typeValidationErrorIntValue
|
|
85
|
+
}).min(0, {
|
|
86
|
+
message: t.typeValidationErrorNegativeLength
|
|
87
|
+
}).optional(),
|
|
88
|
+
maxContains: zod.number().int({
|
|
89
|
+
message: t.typeValidationErrorIntValue
|
|
90
|
+
}).min(0, {
|
|
91
|
+
message: t.typeValidationErrorNegativeLength
|
|
92
|
+
}).optional()
|
|
93
|
+
}).refine(({ minItems, maxItems })=>refineRangeConsistency(minItems, false, maxItems, false), {
|
|
94
|
+
message: t.arrayValidationErrorMinMax,
|
|
95
|
+
path: [
|
|
96
|
+
"minmax"
|
|
97
|
+
]
|
|
98
|
+
}).refine(({ minContains, maxContains })=>refineRangeConsistency(minContains, false, maxContains, false), {
|
|
99
|
+
message: t.arrayValidationErrorContainsMinMax,
|
|
100
|
+
path: [
|
|
101
|
+
"minmaxContains"
|
|
102
|
+
]
|
|
103
|
+
});
|
|
104
|
+
const getJsonObjectType = (t)=>zod.object({
|
|
105
|
+
minProperties: zod.number().int({
|
|
106
|
+
message: t.typeValidationErrorIntValue
|
|
107
|
+
}).min(0, {
|
|
108
|
+
message: t.typeValidationErrorNegativeLength
|
|
109
|
+
}).optional(),
|
|
110
|
+
maxProperties: zod.number().int({
|
|
111
|
+
message: t.typeValidationErrorIntValue
|
|
112
|
+
}).min(0, {
|
|
113
|
+
message: t.typeValidationErrorNegativeLength
|
|
114
|
+
}).optional()
|
|
115
|
+
}).refine(({ minProperties, maxProperties })=>refineRangeConsistency(minProperties, false, maxProperties, false), {
|
|
116
|
+
message: t.objectValidationErrorMinMax,
|
|
117
|
+
path: [
|
|
118
|
+
"minmax"
|
|
119
|
+
]
|
|
120
|
+
});
|
|
121
|
+
function getTypeValidation(type, t) {
|
|
122
|
+
const jsonTypesValidation = {
|
|
123
|
+
string: getJsonStringType(t),
|
|
124
|
+
number: getJsonNumberType(t),
|
|
125
|
+
array: getJsonArrayType(t),
|
|
126
|
+
object: getJsonObjectType(t)
|
|
127
|
+
};
|
|
128
|
+
return jsonTypesValidation[type] || zod.any();
|
|
129
|
+
}
|
|
130
|
+
function validateSchemaByType(schema, type, t) {
|
|
131
|
+
const zodSchema = getTypeValidation(type, t);
|
|
132
|
+
const result = zodSchema.safeParse(schema);
|
|
133
|
+
if (result.success) return {
|
|
134
|
+
success: true
|
|
135
|
+
};
|
|
136
|
+
return {
|
|
137
|
+
success: false,
|
|
138
|
+
errors: result.error.issues
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
function buildValidationTree(schema, t) {
|
|
142
|
+
const deriveType = (sch)=>{
|
|
143
|
+
if (!sch || "object" != typeof sch) return;
|
|
144
|
+
const declared = sch.type;
|
|
145
|
+
if ("string" == typeof declared) return declared;
|
|
146
|
+
if (Array.isArray(declared) && declared.length > 0 && "string" == typeof declared[0]) return declared[0];
|
|
147
|
+
};
|
|
148
|
+
if ("boolean" == typeof schema) {
|
|
149
|
+
const validation = true === schema ? {
|
|
150
|
+
success: true
|
|
151
|
+
} : {
|
|
152
|
+
success: false,
|
|
153
|
+
errors: [
|
|
154
|
+
{
|
|
155
|
+
code: "custom",
|
|
156
|
+
message: t.validatorErrorSchemaValidation,
|
|
157
|
+
path: []
|
|
158
|
+
}
|
|
159
|
+
]
|
|
160
|
+
};
|
|
161
|
+
const node = {
|
|
162
|
+
name: String(schema),
|
|
163
|
+
validation,
|
|
164
|
+
children: {},
|
|
165
|
+
cumulativeChildrenErrors: validation.success ? 0 : validation.errors?.length ?? 0
|
|
166
|
+
};
|
|
167
|
+
return node;
|
|
168
|
+
}
|
|
169
|
+
const sch = schema;
|
|
170
|
+
const currentType = deriveType(sch);
|
|
171
|
+
const validation = validateSchemaByType(schema, currentType, t);
|
|
172
|
+
const children = {};
|
|
173
|
+
if ("object" === currentType) {
|
|
174
|
+
const properties = sch.properties;
|
|
175
|
+
if (properties && "object" == typeof properties) for (const [propName, propSchema] of Object.entries(properties))children[propName] = buildValidationTree(propSchema, t);
|
|
176
|
+
if (sch.patternProperties && "object" == typeof sch.patternProperties) for (const [patternName, patternSchema] of Object.entries(sch.patternProperties))children[`pattern:${patternName}`] = buildValidationTree(patternSchema, t);
|
|
177
|
+
}
|
|
178
|
+
if ("array" === currentType) {
|
|
179
|
+
const items = sch.items;
|
|
180
|
+
if (Array.isArray(items)) items.forEach((it, idx)=>{
|
|
181
|
+
children[`items[${idx}]`] = buildValidationTree(it, t);
|
|
182
|
+
});
|
|
183
|
+
else if (items) children.items = buildValidationTree(items, t);
|
|
184
|
+
if (Array.isArray(sch.prefixItems)) sch.prefixItems.forEach((it, idx)=>{
|
|
185
|
+
children[`prefixItems[${idx}]`] = buildValidationTree(it, t);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
const combinators = [
|
|
189
|
+
"allOf",
|
|
190
|
+
"anyOf",
|
|
191
|
+
"oneOf"
|
|
192
|
+
];
|
|
193
|
+
for (const comb of combinators){
|
|
194
|
+
const arr = sch[comb];
|
|
195
|
+
if (Array.isArray(arr)) arr.forEach((subSchema, idx)=>{
|
|
196
|
+
children[[
|
|
197
|
+
comb,
|
|
198
|
+
idx
|
|
199
|
+
].join(":")] = buildValidationTree(subSchema, t);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
if (sch.not) children.not = buildValidationTree(sch.not, t);
|
|
203
|
+
if (sch.$defs && "object" == typeof sch.$defs) for (const [defName, defSchema] of Object.entries(sch.$defs))children[`$defs:${defName}`] = buildValidationTree(defSchema, t);
|
|
204
|
+
const definitions = sch.definitions;
|
|
205
|
+
if (definitions && "object" == typeof definitions) for (const [defName, defSchema] of Object.entries(definitions))children[`definitions:${defName}`] = buildValidationTree(defSchema, t);
|
|
206
|
+
const ownErrors = validation.success ? 0 : validation.errors?.length ?? 0;
|
|
207
|
+
const childrenErrors = Object.values(children).reduce((sum, child)=>sum + child.cumulativeChildrenErrors, 0);
|
|
208
|
+
return {
|
|
209
|
+
name: currentType,
|
|
210
|
+
validation,
|
|
211
|
+
children,
|
|
212
|
+
cumulativeChildrenErrors: ownErrors + childrenErrors
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
export { buildValidationTree, getTypeValidation, validateSchemaByType };
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import ajv from "ajv";
|
|
2
|
+
import ajv_formats from "ajv-formats";
|
|
3
|
+
import { compileDependentEnums } from "../lib/schemaCompile.js";
|
|
4
|
+
const jsonValidator_ajv = new ajv({
|
|
5
|
+
allErrors: true,
|
|
6
|
+
strict: false,
|
|
7
|
+
validateSchema: false,
|
|
8
|
+
validateFormats: false
|
|
9
|
+
});
|
|
10
|
+
ajv_formats(jsonValidator_ajv);
|
|
11
|
+
function findLineNumberForPath(jsonStr, path) {
|
|
12
|
+
try {
|
|
13
|
+
if ("/" === path || "" === path) return {
|
|
14
|
+
line: 1,
|
|
15
|
+
column: 1
|
|
16
|
+
};
|
|
17
|
+
const pathSegments = path.split("/").filter(Boolean);
|
|
18
|
+
if (0 === pathSegments.length) return {
|
|
19
|
+
line: 1,
|
|
20
|
+
column: 1
|
|
21
|
+
};
|
|
22
|
+
const lines = jsonStr.split("\n");
|
|
23
|
+
if (1 === pathSegments.length) {
|
|
24
|
+
const propName = pathSegments[0];
|
|
25
|
+
const propPattern = new RegExp(`([\\s]*)("${propName}")`);
|
|
26
|
+
for(let i = 0; i < lines.length; i++){
|
|
27
|
+
const line = lines[i];
|
|
28
|
+
const match = propPattern.exec(line);
|
|
29
|
+
if (match) {
|
|
30
|
+
const columnPos = line.indexOf(`"${propName}"`) + 1;
|
|
31
|
+
return {
|
|
32
|
+
line: i + 1,
|
|
33
|
+
column: columnPos
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (pathSegments.length > 1) {
|
|
39
|
+
if ("/aa/a" === path) {
|
|
40
|
+
let parentFound = false;
|
|
41
|
+
let lineWithNestedProp = -1;
|
|
42
|
+
for(let i = 0; i < lines.length; i++){
|
|
43
|
+
const line = lines[i];
|
|
44
|
+
if (line.includes(`"${pathSegments[0]}"`)) {
|
|
45
|
+
parentFound = true;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (parentFound && line.includes(`"${pathSegments[1]}"`)) {
|
|
49
|
+
lineWithNestedProp = i;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (-1 !== lineWithNestedProp) {
|
|
54
|
+
const line = lines[lineWithNestedProp];
|
|
55
|
+
const column = line.indexOf(`"${pathSegments[1]}"`) + 1;
|
|
56
|
+
return {
|
|
57
|
+
line: lineWithNestedProp + 1,
|
|
58
|
+
column: column
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const lastSegment = pathSegments[pathSegments.length - 1];
|
|
63
|
+
for(let i = 0; i < lines.length; i++){
|
|
64
|
+
const line = lines[i];
|
|
65
|
+
if (line.includes(`"${lastSegment}"`)) {
|
|
66
|
+
const column = line.indexOf(`"${lastSegment}"`) + 1;
|
|
67
|
+
return {
|
|
68
|
+
line: i + 1,
|
|
69
|
+
column: column
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.error("Error finding line number:", error);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function extractErrorPosition(error, jsonInput) {
|
|
81
|
+
let line = 1;
|
|
82
|
+
let column = 1;
|
|
83
|
+
const errorMessage = error.message;
|
|
84
|
+
const lineColMatch = errorMessage.match(/at line (\d+) column (\d+)/);
|
|
85
|
+
if (lineColMatch?.[1] && lineColMatch?.[2]) {
|
|
86
|
+
line = Number.parseInt(lineColMatch[1], 10);
|
|
87
|
+
column = Number.parseInt(lineColMatch[2], 10);
|
|
88
|
+
} else {
|
|
89
|
+
const positionMatch = errorMessage.match(/position (\d+)/);
|
|
90
|
+
if (positionMatch?.[1]) {
|
|
91
|
+
const position = Number.parseInt(positionMatch[1], 10);
|
|
92
|
+
const jsonUpToError = jsonInput.substring(0, position);
|
|
93
|
+
const lines = jsonUpToError.split("\n");
|
|
94
|
+
line = lines.length;
|
|
95
|
+
column = lines[lines.length - 1].length + 1;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
line,
|
|
100
|
+
column
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function validateJson(jsonInput, schema) {
|
|
104
|
+
if (!jsonInput.trim()) return {
|
|
105
|
+
valid: false,
|
|
106
|
+
errors: [
|
|
107
|
+
{
|
|
108
|
+
path: "/",
|
|
109
|
+
message: "Empty JSON input"
|
|
110
|
+
}
|
|
111
|
+
]
|
|
112
|
+
};
|
|
113
|
+
try {
|
|
114
|
+
const jsonObject = JSON.parse(jsonInput);
|
|
115
|
+
const compiledSchema = compileDependentEnums(schema);
|
|
116
|
+
const validate = jsonValidator_ajv.compile(compiledSchema);
|
|
117
|
+
const valid = validate(jsonObject);
|
|
118
|
+
if (!valid) {
|
|
119
|
+
const errors = validate.errors?.map((error)=>{
|
|
120
|
+
const path = error.instancePath || "/";
|
|
121
|
+
const position = findLineNumberForPath(jsonInput, path);
|
|
122
|
+
return {
|
|
123
|
+
path,
|
|
124
|
+
message: error.message || "Unknown error",
|
|
125
|
+
line: position?.line,
|
|
126
|
+
column: position?.column
|
|
127
|
+
};
|
|
128
|
+
}) || [];
|
|
129
|
+
return {
|
|
130
|
+
valid: false,
|
|
131
|
+
errors
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
valid: true,
|
|
136
|
+
errors: []
|
|
137
|
+
};
|
|
138
|
+
} catch (error) {
|
|
139
|
+
if (!(error instanceof Error)) return {
|
|
140
|
+
valid: false,
|
|
141
|
+
errors: [
|
|
142
|
+
{
|
|
143
|
+
path: "/",
|
|
144
|
+
message: `Unknown error: ${error}`
|
|
145
|
+
}
|
|
146
|
+
]
|
|
147
|
+
};
|
|
148
|
+
const { line, column } = extractErrorPosition(error, jsonInput);
|
|
149
|
+
return {
|
|
150
|
+
valid: false,
|
|
151
|
+
errors: [
|
|
152
|
+
{
|
|
153
|
+
path: "/",
|
|
154
|
+
message: error.message,
|
|
155
|
+
line,
|
|
156
|
+
column
|
|
157
|
+
}
|
|
158
|
+
]
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
export { extractErrorPosition, findLineNumberForPath, validateJson };
|
package/package.json
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@datenlotse/jsonjoy-builder",
|
|
3
|
+
"license": "MIT",
|
|
4
|
+
"description": "Visual JSON Schema editor for creating and manipulating JSON Schema definitions with an intuitive interface.",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Ophir LOJKINE",
|
|
7
|
+
"url": "http://ophir.dev/"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"json",
|
|
11
|
+
"schema",
|
|
12
|
+
"editor",
|
|
13
|
+
"visual",
|
|
14
|
+
"react"
|
|
15
|
+
],
|
|
16
|
+
"private": false,
|
|
17
|
+
"version": "0.4.0",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/lovasoa/jsonjoy-builder.git"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"!tsconfig.tsbuildinfo",
|
|
27
|
+
"!tsconfig.js"
|
|
28
|
+
],
|
|
29
|
+
"main": "./dist/index.js",
|
|
30
|
+
"module": "./dist/index.js",
|
|
31
|
+
"types": "./dist/jsonjoy-builder.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/jsonjoy-builder.d.ts",
|
|
35
|
+
"import": "./dist/index.js",
|
|
36
|
+
"module": "./dist/index.js",
|
|
37
|
+
"default": "./dist/index.js"
|
|
38
|
+
},
|
|
39
|
+
"./styles.css": {
|
|
40
|
+
"import": "./dist/index.css",
|
|
41
|
+
"module": "./dist/index.css",
|
|
42
|
+
"default": "./dist/index.css"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"dev": "rsbuild dev",
|
|
47
|
+
"preview": "rsbuild preview",
|
|
48
|
+
"build": "rslib build",
|
|
49
|
+
"build:demo": "rsbuild build",
|
|
50
|
+
"build:dev": "rslib build --mode development",
|
|
51
|
+
"lint": "biome lint .",
|
|
52
|
+
"format": "biome format . --write",
|
|
53
|
+
"check": "biome check .",
|
|
54
|
+
"fix": "biome check --fix --unsafe",
|
|
55
|
+
"typecheck": "tsc --build --force",
|
|
56
|
+
"test": "tsx --tsconfig tsconfig.test.json --test test/**/*.test.{js,ts,tsx}",
|
|
57
|
+
"test:snapshots": "tsx --test-update-snapshots --tsconfig tsconfig.test.json --test test/**/*.test.{js,ts,tsx}",
|
|
58
|
+
"prepack": "npm run build"
|
|
59
|
+
},
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"@monaco-editor/react": "^4.7.0",
|
|
62
|
+
"@radix-ui/react-checkbox": "^1.3.3",
|
|
63
|
+
"@radix-ui/react-dialog": "^1.1.15",
|
|
64
|
+
"@radix-ui/react-label": "^2.1.7",
|
|
65
|
+
"@radix-ui/react-popover": "^1.1.15",
|
|
66
|
+
"@radix-ui/react-select": "^2.2.6",
|
|
67
|
+
"@radix-ui/react-separator": "^1.1.7",
|
|
68
|
+
"@radix-ui/react-slot": "^1.2.3",
|
|
69
|
+
"@radix-ui/react-switch": "^1.2.6",
|
|
70
|
+
"@radix-ui/react-tabs": "^1.1.13",
|
|
71
|
+
"@radix-ui/react-toast": "^1.2.15",
|
|
72
|
+
"@radix-ui/react-tooltip": "^1.2.8",
|
|
73
|
+
"ajv": "^8.17.1",
|
|
74
|
+
"ajv-formats": "^3.0.1",
|
|
75
|
+
"class-variance-authority": "^0.7.1",
|
|
76
|
+
"clsx": "^2.1.1",
|
|
77
|
+
"lucide-react": "^0.562.0",
|
|
78
|
+
"tailwind-merge": "^3.3.1",
|
|
79
|
+
"zod": "^4.0.17"
|
|
80
|
+
},
|
|
81
|
+
"peerDependencies": {
|
|
82
|
+
"monaco-editor": ">= 0.25.0 < 1",
|
|
83
|
+
"react": ">=19.0.0",
|
|
84
|
+
"react-dom": ">=19.0.0"
|
|
85
|
+
},
|
|
86
|
+
"devDependencies": {
|
|
87
|
+
"@biomejs/biome": "2.3.10",
|
|
88
|
+
"@microsoft/api-extractor": "^7.52.11",
|
|
89
|
+
"@rsbuild/core": "^1.4.15",
|
|
90
|
+
"@rsbuild/plugin-react": "^1.3.5",
|
|
91
|
+
"@rslib/core": "^0.19.0",
|
|
92
|
+
"@tailwindcss/postcss": "^4.1.12",
|
|
93
|
+
"@tanstack/react-query": "^5.85.0",
|
|
94
|
+
"@testing-library/react": "^16.3.0",
|
|
95
|
+
"@types/node": "^25.0.3",
|
|
96
|
+
"@types/react": "^19.1.10",
|
|
97
|
+
"@types/react-dom": "^19.1.7",
|
|
98
|
+
"global-jsdom": "^27.0.0",
|
|
99
|
+
"monaco-editor": "^0.55.1",
|
|
100
|
+
"next-themes": "^0.4.6",
|
|
101
|
+
"postcss": "^8.5.6",
|
|
102
|
+
"react": "^19.1.1",
|
|
103
|
+
"react-dom": "^19.1.1",
|
|
104
|
+
"react-router-dom": "^7.8.1",
|
|
105
|
+
"sonner": "^2.0.7",
|
|
106
|
+
"tailwindcss": "^4.1.12",
|
|
107
|
+
"tailwindcss-animate": "^1.0.7",
|
|
108
|
+
"tsx": "^4.20.6",
|
|
109
|
+
"typescript": "^5.9.2"
|
|
110
|
+
},
|
|
111
|
+
"packageManager": "npm@10.9.3+sha512.e84875bb943e908557780f1eee5d9cfc7a67145730ae4b77ef10ccba30f96ded6096859af69ea3dc5b2fde60725d79aa247cbed9c12544c30bf28a4d4fbc4825"
|
|
112
|
+
}
|