Skip to content

Commit bd26a04

Browse files
committed
Add json schema for Custom Nodes
1 parent 1c01e1f commit bd26a04

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"$id": "Superhero array schema",
3+
"type": "array",
4+
"items": {
5+
"type": "object",
6+
"properties": {
7+
"name": {
8+
"type": "string"
9+
},
10+
"dateOfBirth": {
11+
"type": "string"
12+
},
13+
"aliases": {
14+
"type": "array",
15+
"items": {
16+
"type": "string"
17+
}
18+
},
19+
"logo": {
20+
"type": "string"
21+
},
22+
"portrayedBy": {
23+
"type": "array",
24+
"items": {
25+
"type": "string"
26+
}
27+
},
28+
"publisher": {
29+
"type": "string"
30+
}
31+
},
32+
"required": ["name", "dateOfBirth", "aliases", "logo", "portrayedBy", "publisher"]
33+
}
34+
}

demo/src/demoData/dataDefinitions.tsx

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ import {
2222
UpdateFunctionProps,
2323
} from '../json-edit-react/src/types'
2424
import { Input } from 'object-property-assigner/build'
25-
import schema from './jsonSchema.json'
25+
import jsonSchema from './jsonSchema.json'
26+
import customNodesSchema from './customNodesSchema.json'
2627
import Ajv from 'ajv'
2728

2829
const ajv = new Ajv()
29-
const validate = ajv.compile(schema)
30+
const validateJsonSchema = ajv.compile(jsonSchema)
31+
const validateCustomNodes = ajv.compile(customNodesSchema)
3032

3133
export interface DemoData {
3234
name: string
@@ -275,10 +277,10 @@ export const demoData: Record<string, DemoData> = {
275277
rootName: 'data',
276278
data: data.jsonSchemaValidation,
277279
onUpdate: ({ newData }, toast) => {
278-
const valid = validate(newData)
280+
const valid = validateJsonSchema(newData)
279281
if (!valid) {
280-
console.log('Errors', validate.errors)
281-
const errorMessage = validate.errors
282+
console.log('Errors', validateJsonSchema.errors)
283+
const errorMessage = validateJsonSchema.errors
282284
?.map((error) => `${error.instancePath}${error.instancePath ? ': ' : ''}${error.message}`)
283285
.join('\n')
284286
toast({
@@ -472,7 +474,11 @@ export const demoData: Record<string, DemoData> = {
472474
<span className="code">onError</span>
473475
</Link>{' '}
474476
function that displays a Toast notification rather than the standard error message when
475-
you enter invalid JSON input.)
477+
you enter invalid JSON input or violate{' '}
478+
<Link href="https://github.com/CarlosNZ/json-edit-react#onerror-function" isExternal>
479+
this JSON schema
480+
</Link>
481+
)
476482
</Text>
477483
<Text>
478484
You can also see how the property count text changes depending on the data. This is using
@@ -504,6 +510,23 @@ export const demoData: Record<string, DemoData> = {
504510
restrictEdit: ({ level }) => level > 0,
505511
restrictAdd: true,
506512
restrictDelete: true,
513+
onUpdate: ({ newData }, toast) => {
514+
const valid = validateCustomNodes(newData)
515+
if (!valid) {
516+
console.log('Errors', validateCustomNodes.errors)
517+
const errorMessage = validateCustomNodes.errors
518+
?.map((error) => `${error.instancePath}${error.instancePath ? ': ' : ''}${error.message}`)
519+
.join('\n')
520+
toast({
521+
title: 'Not compliant with JSON Schema',
522+
description: errorMessage,
523+
status: 'error',
524+
duration: 5000,
525+
isClosable: true,
526+
})
527+
return 'JSON Schema error'
528+
}
529+
},
507530
onError: (errorData) => errorData.error.message,
508531
showErrorMessages: false,
509532
customNodeDefinitions: [

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@
7474
"json-view",
7575
"json-viewer",
7676
"json-inspector",
77+
"json-schema",
7778
"react-component",
7879
"react-json",
80+
"react18-json-view",
81+
"react-json-view",
7982
"theme",
8083
"tree",
8184
"tree-view",

0 commit comments

Comments
 (0)