sindri/source/conf.ts
2023-05-11 12:59:02 +02:00

186 lines
3.8 KiB
TypeScript

/**
* @todo generate generic
*/
function input_schema(
) : any
{
return {
"type": "object",
"additionalProperties": false,
"properties": {
"domains": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"description": {
"type": ["null", "string"],
"default": null
},
"key_field": {
"type": ["null","object"],
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"description": {
"type": ["null", "string"],
"default": null
}
},
"required": [
"name"
],
"default": null
},
"data_fields": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"description": {
"type": ["null", "string"],
"default": null
},
"type": {
"type": "string",
"enum": [
"boolean",
"integer",
"float",
"string_short",
"string_medium",
"string_long"
]
},
"nullable": {
"type": "boolean",
"default": true
},
"default": {
"type": ["null", "boolean", "integer", "float", "string"],
"default": null
}
},
"required": [
"name",
"type"
]
},
"default": []
},
"constraints": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [
"unique",
"foreign_key"
]
},
"parameters": {
"type": "object",
"additionalProperties": "string",
"properties": {
},
"required": [
]
}
},
"required": [
"kind"
]
},
"default": []
}
},
"required": [
"name"
]
}
}
},
"required": [
"domains"
]
}
}
/**
*/
function input_normalize(
input_raw : any
) : type_input
{
// validate
if (! input_raw.hasOwnProperty("domains")) {
throw (new Error("input node is missing mandatory field 'domains'"));
}
else {
// sanitize
return {
"domains": (
input_raw["domains"]
.map(
domain_raw => ({
"name": domain_raw["name"],
"description": (domain_raw["description"] ?? null),
"key_field": (
(domain_raw.hasOwnProperty("key_field") && (domain_raw["key_field"] !== null))
? {
"name": domain_raw["key_field"]["name"],
"description": (domain_raw["key_field"]["description"] ?? null),
}
: null
),
"data_fields": (
(domain_raw.hasOwnProperty("data_fields") && (domain_raw["data_fields"] !== null))
? (
domain_raw["data_fields"]
.map(
data_field_raw => ({
"name": data_field_raw["name"],
"description": (data_field_raw["description"] ?? null),
"type": data_field_raw["type"],
"nullable": (data_field_raw["nullable"] ?? true),
"default": data_field_raw["default"],
})
)
)
: []
),
"constraints": (
(domain_raw.hasOwnProperty("constraints") && (domain_raw["constraints"] !== null))
? (
domain_raw["constraints"]
.map(
constraint_raw => ({
"kind": constraint_raw["kind"],
"parameters": (constraint_raw["parameters"] ?? {}),
})
)
)
: []
),
})
)
),
};
}
}