From 37ed5dc1156e4f74d656952ace0de6503b27005a Mon Sep 17 00:00:00 2001 From: natzcam Date: Fri, 20 Dec 2024 01:27:02 +0800 Subject: [PATCH 1/5] validation --- .gitignore | 1 + index.js | 11 +++++++---- lib/codegen.js | 17 ++++++++++++++++- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 323e2c5..98776a6 100644 --- a/.gitignore +++ b/.gitignore @@ -130,3 +130,4 @@ dist .pnp.* package-lock.json +.vscode diff --git a/index.js b/index.js index cb347b2..19298f6 100644 --- a/index.js +++ b/index.js @@ -134,6 +134,7 @@ class Struct extends ResolvedType { this.fields = [] this.fieldsByName = new Map() + this.requiredFields = [] this.optionals = [] this.flagsPosition = -1 @@ -184,6 +185,8 @@ class Struct extends ResolvedType { if (this.flagsPosition === -1) { this.flagsPosition = i } + } else { + this.requiredFields.push(field) } } } @@ -290,11 +293,11 @@ module.exports = class Hyperschema { return json } - toCode () { - return generateCode(this) + toCode (opts = {}) { + return generateCode(this, opts) } - static toDisk (hyperschema, dir) { + static toDisk (hyperschema, dir, opts = {}) { if (!dir) dir = hyperschema.dir fs.mkdirSync(dir, { recursive: true }) @@ -302,7 +305,7 @@ module.exports = class Hyperschema { const codePath = p.join(p.resolve(dir), CODE_FILE_NAME) fs.writeFileSync(jsonPath, JSON.stringify(hyperschema.toJSON(), null, 2), { encoding: 'utf-8' }) - fs.writeFileSync(codePath, hyperschema.toCode(), { encoding: 'utf-8' }) + fs.writeFileSync(codePath, hyperschema.toCode(opts), { encoding: 'utf-8' }) } static from (json) { diff --git a/lib/codegen.js b/lib/codegen.js index 94ebca1..e38fa43 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -1,7 +1,7 @@ const gen = require('generate-object-property') const s = require('generate-string') -module.exports = function generateSchema (hyperschema) { +module.exports = function generateSchema (hyperschema, opts = {}) { const structs = [] const structsByName = new Map() @@ -108,6 +108,9 @@ module.exports = function generateSchema (hyperschema) { str += `// ${struct.fqn}\n` str += `const ${id} = {\n` str += ' preencode (state, m) {\n' + if (opts.debug) { + str += generateValidation(struct) + } str += `${preencode}\n` str += ' },\n' str += ' encode (state, m) {\n' @@ -119,6 +122,18 @@ module.exports = function generateSchema (hyperschema) { str += '}\n' return str + function generateValidation (struct) { + let str = '' + if (struct.requiredFields.length) str += ' const errors = []\n' + for (const field of struct.requiredFields) { + if (field.required) { + str += ` if (${gen('m', field.name)} === undefined || ${gen('m', field.name)} === null) errors.push('${field.name}')\n` + } + } + if (struct.requiredFields.length) str += ` if(errors.length) throw new Error(\`${struct.fqn}[\${errors.join(',')}] \${errors.length>1?'are':'is'} required\`)\n` + return str + } + function generateEncode (struct, { preencode = false } = {}) { const fn = preencode ? 'preencode' : 'encode' let str = '' From e882b18add19a9ffe73827363e3be132ed00f10f Mon Sep 17 00:00:00 2001 From: natzcam Date: Fri, 20 Dec 2024 01:38:31 +0800 Subject: [PATCH 2/5] remove check --- lib/codegen.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/codegen.js b/lib/codegen.js index e38fa43..dbe8538 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -126,9 +126,7 @@ module.exports = function generateSchema (hyperschema, opts = {}) { let str = '' if (struct.requiredFields.length) str += ' const errors = []\n' for (const field of struct.requiredFields) { - if (field.required) { - str += ` if (${gen('m', field.name)} === undefined || ${gen('m', field.name)} === null) errors.push('${field.name}')\n` - } + str += ` if (${gen('m', field.name)} === undefined || ${gen('m', field.name)} === null) errors.push('${field.name}')\n` } if (struct.requiredFields.length) str += ` if(errors.length) throw new Error(\`${struct.fqn}[\${errors.join(',')}] \${errors.length>1?'are':'is'} required\`)\n` return str From b30c36223d93dbb32e5d441c15891738548f071f Mon Sep 17 00:00:00 2001 From: natzcam Date: Fri, 20 Dec 2024 01:40:02 +0800 Subject: [PATCH 3/5] simplify --- lib/codegen.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/codegen.js b/lib/codegen.js index dbe8538..e67721a 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -124,11 +124,13 @@ module.exports = function generateSchema (hyperschema, opts = {}) { function generateValidation (struct) { let str = '' - if (struct.requiredFields.length) str += ' const errors = []\n' + if (!struct.requiredFields.length) return + + str += ' const errors = []\n' for (const field of struct.requiredFields) { str += ` if (${gen('m', field.name)} === undefined || ${gen('m', field.name)} === null) errors.push('${field.name}')\n` } - if (struct.requiredFields.length) str += ` if(errors.length) throw new Error(\`${struct.fqn}[\${errors.join(',')}] \${errors.length>1?'are':'is'} required\`)\n` + str += ` if(errors.length) throw new Error(\`${struct.fqn}[\${errors.join(',')}] \${errors.length>1?'are':'is'} required\`)\n` return str } From bd0871d6ab3c9c4abf984119fc49435308d0c05e Mon Sep 17 00:00:00 2001 From: natzcam Date: Fri, 20 Dec 2024 01:41:06 +0800 Subject: [PATCH 4/5] simplify --- lib/codegen.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/codegen.js b/lib/codegen.js index e67721a..06d5939 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -123,9 +123,9 @@ module.exports = function generateSchema (hyperschema, opts = {}) { return str function generateValidation (struct) { - let str = '' - if (!struct.requiredFields.length) return + if (!struct.requiredFields.length) return '' + let str = '' str += ' const errors = []\n' for (const field of struct.requiredFields) { str += ` if (${gen('m', field.name)} === undefined || ${gen('m', field.name)} === null) errors.push('${field.name}')\n` From a0f9a2fb969a4c2eedd30f57a359045f03e82a4b Mon Sep 17 00:00:00 2001 From: natzcam Date: Fri, 20 Dec 2024 01:43:43 +0800 Subject: [PATCH 5/5] add a new line --- lib/codegen.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/codegen.js b/lib/codegen.js index 06d5939..46eb7ba 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -130,7 +130,7 @@ module.exports = function generateSchema (hyperschema, opts = {}) { for (const field of struct.requiredFields) { str += ` if (${gen('m', field.name)} === undefined || ${gen('m', field.name)} === null) errors.push('${field.name}')\n` } - str += ` if(errors.length) throw new Error(\`${struct.fqn}[\${errors.join(',')}] \${errors.length>1?'are':'is'} required\`)\n` + str += ` if(errors.length) throw new Error(\`${struct.fqn}[\${errors.join(',')}] \${errors.length>1?'are':'is'} required\`)\n\n` return str }