@@ -22,11 +22,13 @@ import {
2222 UpdateFunctionProps ,
2323} from '../json-edit-react/src/types'
2424import { Input } from 'object-property-assigner/build'
25- import schema from './jsonSchema.json'
25+ import jsonSchema from './jsonSchema.json'
26+ import customNodesSchema from './customNodesSchema.json'
2627import Ajv from 'ajv'
2728
2829const ajv = new Ajv ( )
29- const validate = ajv . compile ( schema )
30+ const validateJsonSchema = ajv . compile ( jsonSchema )
31+ const validateCustomNodes = ajv . compile ( customNodesSchema )
3032
3133export 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 : [
0 commit comments