import Joi from "@stirling-tools/joi";
import { Fragment } from "react";
interface GenericFieldProps {
fieldName: string,
joiDefinition: Joi.Description
}
interface Flags {
label: string,
description: string,
}
export function GenericField({ fieldName, joiDefinition }: GenericFieldProps) {
const flags = joiDefinition.flags as Flags;
switch (joiDefinition.type) {
case "number":
var validValues = joiDefinition.allow;
if(validValues) { // Restrained number input
return (
{flags.label}:
{joiDefinition.allow.map((e: string) => {
return ()
})}
);
}
else { // Unrestrained number input
// TODO: Check if integer or not.
return (
{flags.label}:
);
}
break;
case "string":
if(joiDefinition.allow) { // Restrained text input
return (
{flags.label}:
{joiDefinition.allow.map((e: string) => {
return ()
})}
);
}
else {
// TODO: Implement unrestrained text input
return (
string, unrestrained text input is not implemented
)
}
break;
case "comma_array":
if(joiDefinition.items.length == 1) {
const item: Joi.Description = joiDefinition.items[0];
if(item.type == "number") {
const props: any = {};
item.rules.forEach((rule: { args: any, name: string}) => {
switch (rule.name) {
case "integer":
if(props.pattern) {
return (props.pattern was already set, this is not implemented.
);
}
props.pattern = `(\\d+)(,\\s*\\d+)*`;
break;
case "min":
// TODO: Could validate this in frontend first.
break;
case "max":
// TODO: Could validate this in frontend first.
break;
default:
return (comma_array, item rule {rule.name} is not implemented.
);
}
});
return (
{flags.label}:
);
}
else {
return (comma_array, other types than numbers are not implemented yet.
);
}
}
else {
// TODO: Implement multiple items if necessary
return (comma_array, joi items are empty or bigger than one, this is not implemented
);
}
break;
case "alternatives":
return (
{flags.label}:
);
default:
console.log(joiDefinition);
return (GenericField.tsx: "{fieldName}": requested type "{joiDefinition.type}" not found. Check console for further info.
)
}
}