masumi-schema-validator-component 0.2.5 → 1.0.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/README.md +273 -57
- package/dist/index.d.mts +2784 -323
- package/dist/index.d.ts +2784 -323
- package/dist/index.js +2019 -275
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2003 -276
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/JobInputRenderer.tsx +32 -148
- package/src/components/JobInputsFormRenderer.tsx +14 -14
- package/src/components/SchemaBuilder.tsx +843 -0
- package/src/components/SchemaPlayground.tsx +213 -51
- package/src/components/input-renderers.tsx +444 -0
- package/src/components/ui-components.tsx +62 -0
- package/src/dev/examples.ts +35 -12
- package/src/dev/main.tsx +4 -3
- package/src/dev/styles.css +54 -0
- package/src/index.ts +1 -0
- package/src/lib/job-input-schema.ts +725 -46
- package/src/lib/validation.ts +278 -41
package/README.md
CHANGED
|
@@ -1,96 +1,312 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Masumi Schema Validator Component
|
|
2
2
|
|
|
3
|
-
React component
|
|
3
|
+
A React component library for validating and previewing job input schemas for the Masumi platform. This component provides a playground interface where you can write, validate, and preview how input schemas will render as forms.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ✅ **Schema Validation**: Real-time validation of JSON schemas against the Masumi Job Input Schema specification
|
|
8
|
+
- 🎨 **Live Preview**: See how your schema renders as a form in real-time
|
|
9
|
+
- 📝 **Monaco Editor**: Syntax-highlighted JSON editor with error detection
|
|
10
|
+
- 🔍 **Detailed Error Messages**: Clear validation errors with line numbers and helpful suggestions
|
|
11
|
+
- 🌓 **Dark Mode Support**: Automatic theme detection and support
|
|
12
|
+
- 📦 **TypeScript**: Fully typed with TypeScript definitions
|
|
13
|
+
- 🎯 **Multiple Schema Formats**: Supports wrapped format, array format, and single schema format
|
|
4
14
|
|
|
5
15
|
## Installation
|
|
6
16
|
|
|
7
17
|
```bash
|
|
8
|
-
npm install
|
|
18
|
+
npm install masumi-schema-validator-component
|
|
9
19
|
```
|
|
10
20
|
|
|
11
|
-
|
|
21
|
+
### Peer Dependencies
|
|
12
22
|
|
|
13
|
-
This
|
|
23
|
+
This package requires the following peer dependencies:
|
|
14
24
|
|
|
15
|
-
|
|
25
|
+
- `react` >= 18
|
|
26
|
+
- `react-dom` >= 18
|
|
27
|
+
- `tailwindcss` >= 3
|
|
16
28
|
|
|
17
|
-
|
|
29
|
+
## Quick Start
|
|
18
30
|
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
31
|
+
```tsx
|
|
32
|
+
import { SchemaPlayground } from 'masumi-schema-validator-component';
|
|
33
|
+
|
|
34
|
+
function App() {
|
|
35
|
+
const examples = [
|
|
36
|
+
{
|
|
37
|
+
label: 'Simple Form',
|
|
38
|
+
value: JSON.stringify([
|
|
39
|
+
{
|
|
40
|
+
id: 'name',
|
|
41
|
+
type: 'text',
|
|
42
|
+
name: 'Full Name',
|
|
43
|
+
data: {
|
|
44
|
+
placeholder: 'Enter your name'
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
])
|
|
48
|
+
}
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<SchemaPlayground
|
|
53
|
+
initialSchema="[]"
|
|
54
|
+
examples={examples}
|
|
55
|
+
onSchemaChange={(schema, isValid) => {
|
|
56
|
+
console.log('Schema changed:', schema, 'Valid:', isValid);
|
|
57
|
+
}}
|
|
58
|
+
/>
|
|
59
|
+
);
|
|
27
60
|
}
|
|
28
61
|
```
|
|
29
62
|
|
|
30
|
-
|
|
63
|
+
## Components
|
|
31
64
|
|
|
32
|
-
|
|
65
|
+
### SchemaPlayground
|
|
33
66
|
|
|
34
|
-
|
|
67
|
+
The main component that provides a split-pane interface with a schema editor on the left and form preview on the right.
|
|
35
68
|
|
|
36
|
-
|
|
69
|
+
#### Props
|
|
37
70
|
|
|
38
|
-
|
|
71
|
+
```typescript
|
|
72
|
+
interface SchemaPlaygroundProps {
|
|
73
|
+
initialSchema?: string; // Initial JSON schema string (default: '[]')
|
|
74
|
+
examples?: Array<{ // Array of example schemas to load
|
|
75
|
+
label: string;
|
|
76
|
+
value: string;
|
|
77
|
+
}>;
|
|
78
|
+
className?: string; // Additional CSS classes
|
|
79
|
+
onSchemaChange?: ( // Callback when schema changes
|
|
80
|
+
schema: string,
|
|
81
|
+
isValid: boolean
|
|
82
|
+
) => void;
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
#### Example
|
|
39
87
|
|
|
40
88
|
```tsx
|
|
41
|
-
|
|
42
|
-
|
|
89
|
+
<SchemaPlayground
|
|
90
|
+
initialSchema={mySchema}
|
|
91
|
+
examples={[
|
|
92
|
+
{ label: 'Example 1', value: '[...]' },
|
|
93
|
+
{ label: 'Example 2', value: '[...]' }
|
|
94
|
+
]}
|
|
95
|
+
onSchemaChange={(schema, isValid) => {
|
|
96
|
+
if (isValid) {
|
|
97
|
+
// Save valid schema
|
|
98
|
+
}
|
|
99
|
+
}}
|
|
100
|
+
/>
|
|
101
|
+
```
|
|
43
102
|
|
|
44
|
-
|
|
45
|
-
const [formData, setFormData] = useState({});
|
|
103
|
+
### JobInputsFormRenderer
|
|
46
104
|
|
|
47
|
-
|
|
48
|
-
// ... your schema items
|
|
49
|
-
];
|
|
105
|
+
Renders a form based on validated job input schemas. This component is used internally by `SchemaPlayground` but can also be used standalone.
|
|
50
106
|
|
|
51
|
-
|
|
52
|
-
<JobInputsFormRenderer
|
|
53
|
-
jobInputSchemas={schemas}
|
|
54
|
-
onFormDataChange={setFormData}
|
|
55
|
-
className="my-custom-class"
|
|
56
|
-
/>
|
|
57
|
-
);
|
|
58
|
-
};
|
|
59
|
-
```
|
|
107
|
+
#### Props
|
|
60
108
|
|
|
61
|
-
|
|
109
|
+
```typescript
|
|
110
|
+
interface JobInputsFormRendererProps {
|
|
111
|
+
jobInputSchemas: JobInputSchemaType[];
|
|
112
|
+
onFormDataChange?: (
|
|
113
|
+
formData: Record<string, string | number | boolean | number[] | null>
|
|
114
|
+
) => void;
|
|
115
|
+
disabled?: boolean;
|
|
116
|
+
className?: string;
|
|
117
|
+
}
|
|
118
|
+
```
|
|
62
119
|
|
|
63
|
-
|
|
120
|
+
#### Example
|
|
64
121
|
|
|
65
122
|
```tsx
|
|
66
|
-
import {
|
|
123
|
+
import { JobInputsFormRenderer } from 'masumi-schema-validator-component';
|
|
67
124
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
/>
|
|
75
|
-
</div>
|
|
76
|
-
);
|
|
77
|
-
};
|
|
125
|
+
<JobInputsFormRenderer
|
|
126
|
+
jobInputSchemas={validatedSchemas}
|
|
127
|
+
onFormDataChange={(data) => {
|
|
128
|
+
console.log('Form data:', data);
|
|
129
|
+
}}
|
|
130
|
+
/>
|
|
78
131
|
```
|
|
79
132
|
|
|
80
|
-
|
|
133
|
+
## Schema Format
|
|
81
134
|
|
|
82
|
-
|
|
135
|
+
The component validates schemas according to the Masumi Job Input Schema specification. Schemas can be provided in three formats:
|
|
83
136
|
|
|
84
|
-
|
|
85
|
-
|
|
137
|
+
### 1. Wrapped Format
|
|
138
|
+
```json
|
|
139
|
+
{
|
|
140
|
+
"input_data": [
|
|
141
|
+
{
|
|
142
|
+
"id": "field1",
|
|
143
|
+
"type": "text",
|
|
144
|
+
"name": "Field 1"
|
|
145
|
+
}
|
|
146
|
+
]
|
|
147
|
+
}
|
|
148
|
+
```
|
|
86
149
|
|
|
87
|
-
|
|
150
|
+
### 2. Array Format
|
|
151
|
+
```json
|
|
152
|
+
[
|
|
153
|
+
{
|
|
154
|
+
"id": "field1",
|
|
155
|
+
"type": "text",
|
|
156
|
+
"name": "Field 1"
|
|
157
|
+
}
|
|
158
|
+
]
|
|
159
|
+
```
|
|
88
160
|
|
|
89
|
-
|
|
161
|
+
### 3. Single Schema Format
|
|
162
|
+
```json
|
|
163
|
+
{
|
|
164
|
+
"id": "field1",
|
|
165
|
+
"type": "text",
|
|
166
|
+
"name": "Field 1"
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Supported Input Types
|
|
171
|
+
|
|
172
|
+
- `text` - Single-line text input
|
|
173
|
+
- `textarea` - Multi-line text input
|
|
174
|
+
- `number` - Number input
|
|
175
|
+
- `email` - Email input with validation
|
|
176
|
+
- `password` - Password input
|
|
177
|
+
- `tel` - Telephone number input
|
|
178
|
+
- `url` - URL input
|
|
179
|
+
- `date` - Date picker
|
|
180
|
+
- `datetime-local` - Date and time picker
|
|
181
|
+
- `time` - Time picker
|
|
182
|
+
- `month` - Month picker
|
|
183
|
+
- `week` - Week picker
|
|
184
|
+
- `color` - Color picker
|
|
185
|
+
- `range` - Range slider
|
|
186
|
+
- `file` - File upload
|
|
187
|
+
- `option` - Select dropdown (single or multi-select)
|
|
188
|
+
- `checkbox` - Checkbox input
|
|
189
|
+
- `radio` - Radio button group
|
|
190
|
+
- `boolean` - Boolean toggle
|
|
191
|
+
- `hidden` - Hidden field
|
|
192
|
+
- `search` - Search input
|
|
193
|
+
- `none` - No input (display only)
|
|
194
|
+
|
|
195
|
+
## Validations
|
|
196
|
+
|
|
197
|
+
Schemas can include validation rules:
|
|
198
|
+
|
|
199
|
+
```json
|
|
200
|
+
{
|
|
201
|
+
"id": "email",
|
|
202
|
+
"type": "email",
|
|
203
|
+
"name": "Email",
|
|
204
|
+
"validations": [
|
|
205
|
+
{ "validation": "format", "value": "email" },
|
|
206
|
+
{ "validation": "min", "value": "5" },
|
|
207
|
+
{ "validation": "max", "value": "100" },
|
|
208
|
+
{ "validation": "optional" }
|
|
209
|
+
]
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Supported Validation Types
|
|
214
|
+
|
|
215
|
+
- `min` - Minimum length or value
|
|
216
|
+
- `max` - Maximum length or value
|
|
217
|
+
- `format` - Format validation (email, url, non-empty, integer)
|
|
218
|
+
- `optional` - Makes the field optional
|
|
219
|
+
- `accept` - File type acceptance (for file inputs)
|
|
220
|
+
|
|
221
|
+
## Validation API
|
|
222
|
+
|
|
223
|
+
### validateSchemaWithZod
|
|
224
|
+
|
|
225
|
+
Validates a JSON schema string and returns validation results.
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
import { validateSchemaWithZod } from 'masumi-schema-validator-component';
|
|
229
|
+
|
|
230
|
+
const result = validateSchemaWithZod(schemaString);
|
|
90
231
|
|
|
91
232
|
if (result.valid) {
|
|
92
|
-
console.log(
|
|
233
|
+
console.log('Valid schemas:', result.parsedSchemas);
|
|
93
234
|
} else {
|
|
94
|
-
console.
|
|
235
|
+
console.log('Errors:', result.errors);
|
|
236
|
+
// Errors include line numbers and helpful messages
|
|
95
237
|
}
|
|
96
238
|
```
|
|
239
|
+
|
|
240
|
+
#### Return Type
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
interface ValidationResult {
|
|
244
|
+
valid: boolean;
|
|
245
|
+
errors: { message: string; line?: number }[];
|
|
246
|
+
parsedSchemas?: JobInputSchemaType[];
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
## Development
|
|
251
|
+
|
|
252
|
+
### Setup
|
|
253
|
+
|
|
254
|
+
```bash
|
|
255
|
+
# Install dependencies
|
|
256
|
+
npm install
|
|
257
|
+
|
|
258
|
+
# Start development server
|
|
259
|
+
npm run dev
|
|
260
|
+
|
|
261
|
+
# Build library
|
|
262
|
+
npm run build
|
|
263
|
+
|
|
264
|
+
# Type check
|
|
265
|
+
npm run type-check
|
|
266
|
+
|
|
267
|
+
# Lint
|
|
268
|
+
npm run lint
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### Project Structure
|
|
272
|
+
|
|
273
|
+
```
|
|
274
|
+
src/
|
|
275
|
+
components/
|
|
276
|
+
SchemaPlayground.tsx # Main playground component
|
|
277
|
+
JobInputsFormRenderer.tsx # Form renderer component
|
|
278
|
+
JobInputRenderer.tsx # Individual input renderer
|
|
279
|
+
lib/
|
|
280
|
+
validation.ts # Schema validation logic
|
|
281
|
+
job-input-schema.ts # Schema type definitions
|
|
282
|
+
dev/
|
|
283
|
+
main.tsx # Development app
|
|
284
|
+
examples.ts # Example schemas
|
|
285
|
+
styles.css # Global styles
|
|
286
|
+
index.ts # Main exports
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
## Styling
|
|
290
|
+
|
|
291
|
+
This component uses Tailwind CSS for styling. Make sure Tailwind CSS is configured in your project. The component includes its own minimal UI components but relies on Tailwind for styling.
|
|
292
|
+
|
|
293
|
+
## TypeScript
|
|
294
|
+
|
|
295
|
+
Full TypeScript support is included. All components and functions are fully typed:
|
|
296
|
+
|
|
297
|
+
```typescript
|
|
298
|
+
import type {
|
|
299
|
+
JobInputSchemaType,
|
|
300
|
+
ValidationResult,
|
|
301
|
+
SchemaPlaygroundProps,
|
|
302
|
+
JobInputsFormRendererProps
|
|
303
|
+
} from 'masumi-schema-validator-component';
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
## License
|
|
307
|
+
|
|
308
|
+
See LICENSE file for details.
|
|
309
|
+
|
|
310
|
+
## Contributing
|
|
311
|
+
|
|
312
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|