GraphicsDataDefinition javascript library
This library contains a few useful tools for javascript developers when working with the Graphics Data Definition.
It can be used in the browser or Node.js.
Installation
npm install graphics-data-definition
How to use
Validating schema and data
import {
validateData,
getDefaultDataFromSchema,
setupSchemaValidator,
GDDSchema
} from 'graphics-data-definition'
import fetch from 'node-fetch'
;(async () => {
const mySchema: GDDSchema = {
"$schema": "https://superflytv.github.io/GraphicsDataDefinition/gdd-meta-schema/v1/schema.json",
"title": "One-Line GFX Template",
"type": "object",
"properties": {
"text0": {
"type": "string",
"gddType": "single-line",
},
"color": {
"type": "string",
"gddType": "color-rrggbb",
"pattern": '^#[0-9a-f]{6}$',
"default": "#000000"
}
},
}
const myData = {
text0: "This is the text!"
}
const schemaValidator = await setupSchemaValidator({
fetch: async (url) => {
return await (await fetch(url)).json()
}
})
const schemaValidateResult = schemaValidator.validate(mySchema)
if (schemaValidateResult === null) console.log('Schema is valid!')
else console.log('Schema is not valid: ' + schemaValidateResult)
const dataValidateResult = validateData(mySchema, myData)
if (dataValidateResult === null) console.log('Data is valid!')
else console.log('Data is not valid: ' + schemaValidateResult)
const defaultData = getDefaultDataFromSchema(mySchema)
console.log('Default Data from schema: ' + JSON.stringify(defaultData))
})().catch(console.error)
Building a GUI
See Reference-GUI for example implementation,
import {
validateData,
getDefaultDataFromSchema,
setupSchemaValidator,
GDDSchema,
GDDTypes
} from 'graphics-data-definition'
function drawGUIComponent(schema: GDDSchemaProperty) {
const gddSchema = schema as GDDTypes
if (gddSchema.type === 'string') {
if (gddSchema.gddType === 'single-line') return drawGUIComponentSingleLine(innerProps)
else if (gddSchema.gddType === 'multi-line') return drawGUIComponentMultiLine(innerProps)
} else if (gddSchema.type === 'integer') {
if (gddSchema.gddType === 'select') return drawGUIComponentSelect(innerProps)
}
const basicType = getBasicType(schema.type)
if (basicType === 'boolean') return drawGUIComponentBoolean(innerProps)
if (basicType === 'string') return drawGUIComponentString(innerProps)
if (basicType === 'number') return drawGUIComponentNumber(innerProps)
if (basicType === 'integer') return drawGUIComponentInteger(innerProps)
if (basicType === 'array') return drawGUIComponentArray(innerProps)
if (basicType === 'object') return drawGUIComponentObject(innerProps)
return drawGUIComponentUnknown({ ...innerProps, basicType })
}