Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions src/appmixer/utils/controls/SchemaPassThrough/SchemaPassThrough.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
'use strict';

const SCHEMA_TYPES = ['object', 'array', 'string', 'number', 'integer', 'boolean', 'null'];

module.exports = {

async receive(context) {

const { schema: schemaString, data } = context.messages.in.content;

if (context.properties.generateOutputPortOptions) {
return this.generateOutputPortOptions(context, schemaString);
}

let output;
if (data !== undefined && data !== null && data !== '') {
output = typeof data === 'string' ? this.tryParseJson(data) : data;
} else {
// No data wired — fall back to the example JSON from the schema field if it
// wasn't a JSON Schema. Handy for testing the flow before upstream is wired.
const parsed = this.tryParseJson(schemaString);
output = this.isJsonSchema(parsed) ? {} : (parsed ?? {});
}

return context.sendJson(output, 'out');
},

generateOutputPortOptions(context, schemaString) {

let parsed;
try {
parsed = JSON.parse(schemaString);
} catch (err) {
return context.sendJson([], 'out');
}

const schema = this.isJsonSchema(parsed) ? parsed : this.inferSchema(parsed);
const options = [];
this.schemaToOptions(schema, '', options);
return context.sendJson(options, 'out');
},

isJsonSchema(value) {

if (!value || typeof value !== 'object' || Array.isArray(value)) return false;
if (value.$schema) return true;
if (typeof value.type !== 'string' || !SCHEMA_TYPES.includes(value.type)) return false;
if (value.type === 'object' && value.properties) return true;
if (value.type === 'array' && value.items) return true;
return false;
},

inferSchema(value) {

if (value === null) return { type: 'null' };
if (Array.isArray(value)) {
return {
type: 'array',
items: value.length > 0 ? this.inferSchema(value[0]) : {}
};
}
if (typeof value === 'object') {
const properties = {};
for (const k of Object.keys(value)) {
properties[k] = this.inferSchema(value[k]);
}
return { type: 'object', properties };
}
switch (typeof value) {
case 'string': return { type: 'string', example: value };
case 'number': return { type: Number.isInteger(value) ? 'integer' : 'number', example: value };
case 'boolean': return { type: 'boolean', example: value };
default: return {};
}
},

// Mirror engine's createOutPortOptions: flat array with dotted paths,
// arrays are pushed as one option carrying the full schema, primitives
// and nested objects get individual entries.
schemaToOptions(schema, parentPath, options) {

if (!schema) return;

if (schema.type === 'array') {
options.push({
label: this.titleFromPath(parentPath) || 'Items',
value: parentPath || 'value',
schema
});
return;
}

const properties = schema.properties;
if (!properties) {
if (parentPath === '') {
options.push({
label: 'Value',
value: 'value',
schema
});
}
return;
}

Object.keys(properties).forEach(prop => {
const path = parentPath ? (parentPath + '.' + prop) : prop;
const propSchema = properties[prop];
if (propSchema.type !== 'array') {
const option = { label: this.titleFromPath(path), value: path };
if (propSchema.type) {
// For objects, only expose the type (no `properties`) — children are
// covered by separate flat dotted-path entries below; including
// `properties` would make the picker render duplicates.
option.schema = propSchema.type === 'object'
? { type: 'object' }
: { type: propSchema.type, ...(propSchema.example !== undefined ? { example: propSchema.example } : {}) };

Check failure on line 116 in src/appmixer/utils/controls/SchemaPassThrough/SchemaPassThrough.js

View workflow job for this annotation

GitHub Actions / build

This line has a length of 130. Maximum allowed is 120
}
options.push(option);
}
this.schemaToOptions(propSchema, path, options);
});
},

titleFromPath(path) {

if (!path) return '';
return String(path)
.split('.')
.map(seg => seg.charAt(0).toUpperCase() + seg.slice(1))
.join('.');
},

tryParseJson(str) {

if (typeof str !== 'string') return str;
try {
return JSON.parse(str);
} catch (err) {
return undefined;
}
}
};
54 changes: 54 additions & 0 deletions src/appmixer/utils/controls/SchemaPassThrough/component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "appmixer.utils.controls.SchemaPassThrough",
"author": "Appmixer <info@appmixer.com>",
"description": "Define an output shape from an example JSON or a JSON Schema, then pass JSON data through with a typed output port for easy variable mapping downstream.",
"icon": "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0Ij48cGF0aCBkPSJNOCAzSDdhMiAyIDAgMCAwLTIgMnY1YTIgMiAwIDAgMS0yIDIgMiAyIDAgMCAxIDIgMnY1YTIgMiAwIDAgMCAyIDJoMU0xNiAzaDFhMiAyIDAgMCAxIDIgMnY1YTIgMiAwIDAgMCAyIDIgMiAyIDAgMCAwLTIgMnY1YTIgMiAwIDAgMS0yIDJoLTFNOSA5aDZNOSAxM2g0TTkgMTdoNiIgZmlsbD0ibm9uZSIgc3Ryb2tlPSIjMDAwMDAwIiBzdHJva2Utd2lkdGg9IjIiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIvPjwvc3ZnPgo=",
"private": false,
"inPorts": [
{
"name": "in",
"maxConnections": 1,
"schema": {
"type": "object",
"properties": {
"schema": { "type": "string" },
"data": {}
},
"required": ["schema"]
},
"inspector": {
"inputs": {
"schema": {
"type": "textarea",
"label": "Schema or Example JSON",
"index": 1,
"tooltip": "Paste either a <a target=\"_blank\" href=\"https://json-schema.org/\">JSON Schema</a> or an example JSON object. The component auto-detects which one and uses it to derive the output port shape so downstream components can map properties easily. Example schema: <code>{\"type\":\"object\",\"properties\":{\"name\":{\"type\":\"string\"},\"age\":{\"type\":\"number\"}}}</code>. Example JSON: <code>{\"name\":\"John\",\"age\":30}</code>."
},
"data": {
"type": "text",
"label": "Data",
"index": 2,
"tooltip": "JSON object to pass through. Wire it from an upstream component (e.g. <code>{{{upstream.payload}}}</code>) or paste a literal JSON value. If left empty and the Schema field contains an example JSON, that example is emitted (useful for testing)."
}
}
}
}
],
"outPorts": [
{
"name": "out",
"source": {
"url": "/component/appmixer/utils/controls/SchemaPassThrough?outPort=out",
"data": {
"properties": {
"generateOutputPortOptions": true
},
"messages": {
"in/schema": "inputs/in/schema",
"in/data": "dummy"
}
}
}
}
]
}
5 changes: 4 additions & 1 deletion src/appmixer/utils/controls/bundle.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "appmixer.utils.controls",
"engine": ">=6.0",
"version": "1.12.3",
"version": "1.13.0",
"changelog": {
"1.0.0": [
"Initial version"
Expand Down Expand Up @@ -107,6 +107,9 @@
],
"1.12.3": [
"Fix missing properties in the schema for `Testing` component."
],
"1.13.0": [
"Added component SchemaPassThrough: define an output shape from an example JSON or JSON Schema for easy variable mapping downstream."
]
}
}
Loading