From 56c867e73669df31e7f967bd4ed74d8e899dc9dc Mon Sep 17 00:00:00 2001 From: Misode Date: Fri, 21 Jun 2019 23:05:10 +0200 Subject: [PATCH] First validation functions --- validate.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 validate.js diff --git a/validate.js b/validate.js new file mode 100644 index 00000000..7b1865a0 --- /dev/null +++ b/validate.js @@ -0,0 +1,65 @@ + +function isString(data) { + return data && typeof data === 'string'; +} + +function isNumber(var) { + return data && typeof data === 'number'; +} + +function isObject(var) { + return data && typeof data === 'object'; +} + +function isArray(var) { + return data && typeof data === 'object' && Array.isArray(data); +} + +function validateRange(data, default) { + if (data === undefined) return false; + if (isObject(data)) { + if (isString(data.type) && var.type.endsWith('binomial')) + if (isNumber(data.n) && isNumber(data.p)) { + return { + type: 'minecraft:binomial', + n: data.n, + p: data.p + }; + } + } + let res = {}; + if (isNumber(data.min)) res.min = data.min; + if (isNumber(data.max)) res.max = data.max; + } + } + return false; +} + +function chooseOption(options, value, default) { + for (option of options) { + if (value === option) { + return value; + } else if('minecraft:' + value === option) { + return 'minecraft:' + value; + } + } + return default; +} + +function validateTable(table) { + let res = {}; + res.type = chooseOption(namespace(['empty', 'entity', 'block', 'chest', 'fishing', 'generic']), table.type, 'minecraft:generic'); + if (isArray(table.pools)) { + res.pools = []; + for (let entry of table.pools) { + res.pools.push(validatePool(pools)); + } + } + return res; +} + +function validatePool() { + let newpool = {}; + + return res; +}