Slide 14
Slide 14 text
Metadataからのコード生成
Metadata
{
objects: [{
name: "Tree",
recursive: true,
properties: [
{ key: "name",
value: { atomics: ["string"] } },
{ key: "children",
value: { arrays: [{
value: /* Tree */,
recursive: true
}] } },
],
}]
}
生成されたコード: typia.validate()
(input) => {
const errors = [];
const $vt = (v, path) => {
if (typeof v !== "object" || v === null) {
errors.push({ path, expected: "Tree" });
return false;
}
let ok = true;
if (typeof v.name !== "string") {
errors.push({ path: `${path}.name`, expected: "string" });
ok = false;
}
if (!Array.isArray(v.children)) {
errors.push({ path: `${path}.children`, expected: "Tree[]" });
ok = false;
} else {
v.children.forEach((c, i) => $vt(c, `${path}.children[${i}]`));
}
return ok;
};
return { success: $vt(input, "$input"), errors };
};
14