Socket
Socket
Sign inDemoInstall

graphics-data-definition

Package Overview
Dependencies
8
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    graphics-data-definition

This library contains a few useful tools for javascript developers when working with the [Graphics Data Definition](https://github.com/SuperFlyTV/GraphicsDataDefinition). It can be used in the browser or Node.js.


Version published
Weekly downloads
18
increased by100%
Maintainers
1
Install size
595 kB
Created
Weekly downloads
 

Readme

Source

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

// Typescript
import {
    validateData,
    getDefaultDataFromSchema,
    setupSchemaValidator,
    GDDSchema
} from 'graphics-data-definition'
import fetch from 'node-fetch'

;(async () => {
// This object represents a GDD Schema that have been read from a graphics template:
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"
        }
    },
}
// This object represents the data that comes from the user input form:
const myData = {
    text0: "This is the text!"
}

// Verify that the schema is valid: -------------------------------------------
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)

// Validate that the data is correct: -----------------------------------------
const dataValidateResult = validateData(mySchema, myData)
if (dataValidateResult === null) console.log('Data is valid!')
else console.log('Data is not valid: ' + schemaValidateResult)

// Generate a default data-object, to use for prefilling: ---------------------
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,

// Typescript
import {
    validateData,
    getDefaultDataFromSchema,
    setupSchemaValidator,
    GDDSchema,
    GDDTypes
} from 'graphics-data-definition'


function drawGUIComponent(schema: GDDSchemaProperty) {
    const gddSchema = schema as GDDTypes

    // Handle GDD Types:
    if (gddSchema.type === 'string') {
        if (gddSchema.gddType === 'single-line') return drawGUIComponentSingleLine(innerProps)
        else if (gddSchema.gddType === 'multi-line') return drawGUIComponentMultiLine(innerProps)
        // etc ...
    } else if (gddSchema.type === 'integer') {
		if (gddSchema.gddType === 'select') return drawGUIComponentSelect(innerProps)
        // etc ...
    } // else if () etc ...


    // Handle basic types:
    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)

    // Fallback:
    return drawGUIComponentUnknown({ ...innerProps, basicType })
}

FAQs

Last updated on 20 Oct 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc