shinkansen-transmission

Shinkansen Transmission

Usage no npm install needed!

<script type="module">
  import shinkansenTransmission from 'https://cdn.skypack.dev/shinkansen-transmission';
</script>

README

shinkansen-transmission

Shinkansen Transmission

Shinkansen Transmission transforms JSON Schemas into a description of a form for Zashiki Karakuri.

Your schema should be dereferenced before it is transformed with Shinkansen Transmission (we recommend json-schema-ref-parser).

Installation

npm i -P shinkansen-transmission

toZashiki

const zashiki = toZashiki(rootSchema, values, params)

The transformer walks the rootSchema and maps fields in values and params to another structure, which it returns.

  • rootSchema is a JSON Schema
  • values is a document valid according to the Schema
  • params are any other parameters for the transformer

The return value is an object with the fields meta and elements.

As you might expect, meta contains fields about the Schema, while elements contains fields to be rendered as HTML. (Shinkansen Transmission doesn't express any opinion on what those elements are to be, but assumes that a field will be rendered as an HTML <form /> element or some component which behaves like one.)

Transformed structure

{
  meta: {
    uri: String,
    name: String,
    type: String /* One of "object" "array" "string" "number" "boolean" "null" */,
    schema: Object,
    rootSchema: Object,
    isRequired: Boolean,
    defaultValue: /* Per `type` */,
    value: /* Per `type` */,
  },
  elements: {
    title: String,
    description: String,
    field: {
      isRequired: Boolean,
      value: /* Per `type` */,
      name: String
    }
  }
}
enum
{
  meta: {
    uri: String,
    name: String,
    type: String /* One of "object" "array" "string" "number" "boolean" "null" */,
    schema: Object,
    rootSchema: Object,
    isRequired: Boolean,
    selectedItems: Array
  },
  elements: {
    title: String,
    description: String,
    enum: {
      isRequired: Boolean,
      selectedItems: Array,
      items: Array,
      name: String
    }
  }
}
anyOf
{
  meta: {
    uri: String,
    name: String,
    type: String /* One of "object" "array" "string" "number" "boolean" "null" */,
    schema: Object,
    rootSchema: Object,
    isRequired: Boolean,
    selectedItems: Array,
  },
  elements: {
    title: String,
    description: String,
    anyOf: {
      isRequired: Boolean,
      selectedItems: Array,
      items: Array,
      name: String
    }
  }
}
oneOf
{
  meta: {
    uri: String,
    name: String,
    type: String /* One of "object" "array" "string" "number" "boolean" "null" */,
    schema: Object,
    rootSchema: Object,
    isRequired: Boolean,
    selectedItems: Array,
  },
  elements: {
    title: String,
    description: String,
    oneOf: {
      required: Boolean,
      selectedItems: Array,
      items: Array,
      name: String
    }
  }
}
allOf
  • array or object
{
  meta: {
    uri: String,
    name: String,
    type: String /* One of "object" "array" "string" "number" "boolean" "null" */,
    schema: Object,
    rootSchema: Object,
    required: Boolean,
  },
  elements: {
    title: String,
    description: String,
    fields: Array
  }
}
  • Any other
{
  meta: {
    uri: String,
    name: String,
    type: String /* One of "object" "array" "string" "number" "boolean" "null" */,
    schema: Object,
    rootSchema: Object,
    required: Boolean,
  },
  elements: {
    title: String,
    description: String,
    field: Object
  }
}

fromHashToDocument

const document = fromHashToDocument(values, rootSchema)

For applications accepting POST data.

The transformer walks the rootSchema and maps fields in values to another structure, which it returns.

  • values is a hash of keys and values
  • rootSchema is a JSON Schema

The return value is an object valid according to the Schema.

POST data is generally represented as a hash of keys and values, where the values are all strings, or arrays containing strings. fromHashToDocument transforms that hash into a document.

rootSchema
{
  type: 'object',
  properties: {
    company: {
      type: 'object',
      properties: {
        name: {
          type: 'string'
        },
        publisher: {
          type: 'object',
          properties: {
            firstName: {
              type: 'string'
            },
            lastName: {
              type: 'string'
            },
            age: {
              type: 'number'
            }
          }
        }
      }
    },
    active: {
      type: 'boolean'
    }
  }
}
values

An object.

{
  'company-name': 'Marvel',
  'company-publisher-firstName': 'Stan',
  'company-publisher-lastName': 'Lee',
  'company-publisher-age': '96',
  active: 'true'
}
document

An object.

{
  company: {
    name: 'Marvel',
    publisher: {
      firstName: 'Stan',
      lastName: 'Lee',
      age: 96
    }
  },
  active: true
}

fromDocumentToHash

const values = fromDocumentToHash(document, rootSchema)

For applications accepting POST data.

The transformer walks the document and maps its fields to another structure, which it returns.

  • document is an object valid according to the Schema
  • rootSchema is a JSON Schema

The return value is a hash of keys and values, where the values are all strings, or arrays containing strings.

POST data is generally represented as a hash of keys and values, where the values are all strings, or arrays containing strings. fromDocumentToHash transforms a document into that hash.

document

An object.

{
  company: {
    name: 'Marvel',
    publisher: {
      firstName: 'Stan',
      lastName: 'Lee',
      age: 96
    }
  },
  active: true
}
rootSchema
{
  type: 'object',
  properties: {
    company: {
      type: 'object',
      properties: {
        name: {
          type: 'string'
        },
        publisher: {
          type: 'object',
          properties: {
            firstName: {
              type: 'string'
            },
            lastName: {
              type: 'string'
            }
            age: {
              type: 'number'
            }
          }
        }
      }
    },
    active: {
      type: 'boolean'
    }
  }
}
values

A hash.

{
  'company-name': 'Marvel',
  'company-publisher-firstName': 'Stan',
  'company-publisher-lastName': 'Lee',
  'company-publisher-age': '96',
  active: 'true'
}