From 8b7d0d7d880c255bf9e0754fcf3d65bbb3d3aca6 Mon Sep 17 00:00:00 2001 From: Ethan Davis Date: Tue, 19 Mar 2019 21:13:02 -0700 Subject: [PATCH] 3.2.0: Verison bump, detect missing elements when building --- lib/types/boolean.js | 1 + lib/types/buffer.js | 1 + lib/types/double.js | 1 + lib/types/float.js | 1 + lib/types/instance.js | 1 + lib/types/int.js | 1 + lib/types/list.js | 1 + lib/types/map.js | 1 + lib/types/string.js | 1 + lib/types/uint.js | 1 + lib/types/varint.js | 2 ++ model/Schema.js | 4 ++++ package.json | 2 +- test.js | 24 ++++++++++++++++++++++++ 14 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/types/boolean.js b/lib/types/boolean.js index 8b989c5..55fe6c8 100644 --- a/lib/types/boolean.js +++ b/lib/types/boolean.js @@ -1,4 +1,5 @@ module.exports = { + 'name': 'boolean', 'parse': (buf, from, data) => { if (buf.length - from < 1) { return { diff --git a/lib/types/buffer.js b/lib/types/buffer.js index 5d80fd8..ae22935 100644 --- a/lib/types/buffer.js +++ b/lib/types/buffer.js @@ -3,6 +3,7 @@ const path = require('path') const varint = require(path.join(__dirname, 'varint.js')) module.exports = { + 'name': 'buffer', 'parse': (buf, from, data) => { if (buf.length - from < 1) { return { diff --git a/lib/types/double.js b/lib/types/double.js index b324fed..31dad93 100644 --- a/lib/types/double.js +++ b/lib/types/double.js @@ -1,4 +1,5 @@ module.exports = { + 'name': 'double', 'parse': (buf, from, data) => { if (buf.length - from < 8) { return { diff --git a/lib/types/float.js b/lib/types/float.js index daf668d..d1fc323 100644 --- a/lib/types/float.js +++ b/lib/types/float.js @@ -1,4 +1,5 @@ module.exports = { + 'name': 'float', 'parse': (buf, from, data) => { if (buf.length - from < 4) { return { diff --git a/lib/types/instance.js b/lib/types/instance.js index 463c747..15b8612 100644 --- a/lib/types/instance.js +++ b/lib/types/instance.js @@ -1,4 +1,5 @@ module.exports = { + 'name': 'instance', 'parse': (buf, from, data) => { const parseResult = data.of.parse(buf, from, { 'returnDetails': true diff --git a/lib/types/int.js b/lib/types/int.js index 793b3f3..a86dd88 100644 --- a/lib/types/int.js +++ b/lib/types/int.js @@ -1,4 +1,5 @@ module.exports = { + 'name': 'int', 'parse': (buf, from, data) => { const readBytes = data.size / 8 diff --git a/lib/types/list.js b/lib/types/list.js index d3861ab..740eaa3 100644 --- a/lib/types/list.js +++ b/lib/types/list.js @@ -3,6 +3,7 @@ const path = require('path') const varint = require(path.join(__dirname, 'varint.js')) module.exports = { + 'name': 'list', 'parse': (buf, from, data) => { if (buf.length - from < 1) { return { diff --git a/lib/types/map.js b/lib/types/map.js index bc548e0..e6c1567 100644 --- a/lib/types/map.js +++ b/lib/types/map.js @@ -32,6 +32,7 @@ const genMapSchema = (data) => { } module.exports = { + 'name': 'map', 'parse': (buf, from, data) => { if (buf.length - from < 1) { return { diff --git a/lib/types/string.js b/lib/types/string.js index 754f9dc..6a16786 100644 --- a/lib/types/string.js +++ b/lib/types/string.js @@ -3,6 +3,7 @@ const path = require('path') const varint = require(path.join(__dirname, 'varint.js')) module.exports = { + 'name': 'string', 'parse': (buf, from, data) => { if (buf.length - from < 1) { return { diff --git a/lib/types/uint.js b/lib/types/uint.js index 3be3e12..061f7a1 100644 --- a/lib/types/uint.js +++ b/lib/types/uint.js @@ -1,4 +1,5 @@ module.exports = { + 'name': 'uint', 'parse': (buf, from, data) => { const readBytes = data.size / 8 diff --git a/lib/types/varint.js b/lib/types/varint.js index 1b4f036..31b36d3 100644 --- a/lib/types/varint.js +++ b/lib/types/varint.js @@ -12,6 +12,8 @@ const calcBytes = (number) => { module.exports = {} +module.exports.name = 'varint' + module.exports.parsePrefix = (buf, from) => { const prefix = buf[from] diff --git a/model/Schema.js b/model/Schema.js index a6f9b16..61b673d 100644 --- a/model/Schema.js +++ b/model/Schema.js @@ -45,6 +45,10 @@ module.exports = class Schema { const writeContent = [] for (let i = 0; i < this.elements.length; i++) { + if (typeof data[this.elements[i].name] === 'undefined') { + throw new Error('Missing element: ' + this.elements[i].type.name + ' ' + this.elements[i].name) + } + writeContent.push({ 'value': data[this.elements[i].name], 'serializerData': this.elements[i] diff --git a/package.json b/package.json index 41af97e..dcacd09 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "protocore", - "version": "3.1.1", + "version": "3.2.0", "description": "Specify and deploy performant binary protocol structures in Node", "main": "index.js", "scripts": { diff --git a/test.js b/test.js index a20c3fa..3d7bfbe 100644 --- a/test.js +++ b/test.js @@ -382,6 +382,30 @@ w.add('Map protospec functionality', (result) => { result(true, 'Validated built data. (Protospec success!)') }) +w.add('Detect missing elements', (result) => { + const testSchema = protospec.importAll(` + + def ping + varint time + + `).ping + + const testData = { + + } + + try { + testSchema.build(testData) + } + catch (err) { + result(true, 'Error as expected.') + + return + } + + result(false, 'Expected error to be thrown.') +}) + w.test() process.stdin.on('data', () => {}) \ No newline at end of file