diff --git a/src/octoprint/printer/profile.py b/src/octoprint/printer/profile.py index d7322550b..6ca79ee52 100644 --- a/src/octoprint/printer/profile.py +++ b/src/octoprint/printer/profile.py @@ -645,6 +645,18 @@ class PrinterProfileManager: value[path[-1]] = converter(value[path[-1]]) + def validate_value(profile, path, validator): + value = profile + for part in path[:-1]: + if not isinstance(value, dict) or part not in value: + raise RuntimeError("%s is not contained in profile" % ".".join(path)) + value = value[part] + + if not isinstance(value, dict) or path[-1] not in value: + raise RuntimeError("%s is not contained in profile" % ".".join(path)) + + return validator(value[path[-1]]) + # convert ints for path in ( ("extruder", "count"), @@ -696,6 +708,21 @@ class PrinterProfileManager: ) return False + # validate volume size range + for path in ( + ("volume", "width"), + ("volume", "depth"), + ("volume", "height"), + ): + if not validate_value(profile, path, lambda x: x > 0 and x < 10000): + return False + + # validate extruder count range + if not validate_value( + profile, ("extruder", "count"), lambda x: x > 0 and x < 100 + ): + return False + # validate form factor if profile["volume"]["formFactor"] not in BedFormFactor.values(): self._logger.warning( diff --git a/src/octoprint/static/js/app/viewmodels/printerprofiles.js b/src/octoprint/static/js/app/viewmodels/printerprofiles.js index 2c94e2426..bc79621f4 100644 --- a/src/octoprint/static/js/app/viewmodels/printerprofiles.js +++ b/src/octoprint/static/js/app/viewmodels/printerprofiles.js @@ -124,6 +124,14 @@ $(function () { return !self.name(); }); + self.sizeInvalid = ko.pureComputed(function () { + return ( + !(self.volumeWidth() > 0 && self.volumeWidth() < 10000) || + !(self.volumeDepth() > 0 && self.volumeDepth() < 10000) || + !(self.volumeHeight() > 0 && self.volumeHeight() < 10000) + ); + }); + self.identifierInvalid = ko.pureComputed(function () { var identifier = self.identifier(); var placeholder = self.identifierPlaceholder(); @@ -165,7 +173,9 @@ $(function () { }); self.valid = function () { - return !self.nameInvalid() && !self.identifierInvalid(); + return ( + !self.nameInvalid() && !self.identifierInvalid() && !self.sizeInvalid() + ); }; self.availableColors = ko.observable([ @@ -273,11 +283,32 @@ $(function () { } return v; }; - var validFloat = function (value, def) { - return valid(value, parseFloat, def); + var runChecks = function (value, def, checks) { + if (checks.gt !== undefined) { + if (!(value > checks.gt)) { + return def; + } + } + if (checks.lt !== undefined) { + if (!(value < checks.gt)) { + return def; + } + } + return value; }; - var validInt = function (value, def) { - return valid(value, parseInt, def); + var validFloat = function (value, def, checks) { + var v = valid(value, parseFloat, def); + if (checks) { + v = runChecks(value, def, checks); + } + return v; + }; + var validInt = function (value, def, checks) { + var v = valid(value, parseInt, def); + if (checks) { + v = runChecks(value, def, checks); + } + return v; }; var profile = { @@ -286,16 +317,26 @@ $(function () { color: self.color(), model: self.model(), volume: { - width: validFloat(self.volumeWidth(), defaultProfile.volume.width), - depth: validFloat(self.volumeDepth(), defaultProfile.volume.depth), - height: validFloat(self.volumeHeight(), defaultProfile.volume.height), + width: validFloat(self.volumeWidth(), defaultProfile.volume.width, { + gt: 0, + lt: 10000 + }), + depth: validFloat(self.volumeDepth(), defaultProfile.volume.depth, { + gt: 0, + lt: 10000 + }), + height: validFloat( + self.volumeHeight(), + defaultProfile.volume.height, + {gt: 0, lt: 10000} + ), formFactor: self.volumeFormFactor(), origin: self.volumeOrigin() }, heatedBed: self.heatedBed(), heatedChamber: self.heatedChamber(), extruder: { - count: parseInt(self.extruders()), + count: runChecks(parseInt(self.extruders()), 1, {gt: 0, lt: 100}), offsets: [[0.0, 0.0]], nozzleDiameter: validFloat( self.nozzleDiameter(), @@ -309,19 +350,27 @@ $(function () { }, axes: { x: { - speed: validInt(self.axisXSpeed(), defaultProfile.axes.x.speed), + speed: validInt(self.axisXSpeed(), defaultProfile.axes.x.speed, { + gt: 0 + }), inverted: self.axisXInverted() }, y: { - speed: validInt(self.axisYSpeed(), defaultProfile.axes.y.speed), + speed: validInt(self.axisYSpeed(), defaultProfile.axes.y.speed, { + gt: 0 + }), inverted: self.axisYInverted() }, z: { - speed: validInt(self.axisZSpeed(), defaultProfile.axes.z.speed), + speed: validInt(self.axisZSpeed(), defaultProfile.axes.z.speed, { + gt: 0 + }), inverted: self.axisZInverted() }, e: { - speed: validInt(self.axisESpeed(), defaultProfile.axes.e.speed), + speed: validInt(self.axisESpeed(), defaultProfile.axes.e.speed, { + gt: 0 + }), inverted: self.axisEInverted() } } diff --git a/src/octoprint/templates/snippets/settings/printerprofiles/profileEditorBuildvolume.jinja2 b/src/octoprint/templates/snippets/settings/printerprofiles/profileEditorBuildvolume.jinja2 index bc8dd35f0..e37505232 100644 --- a/src/octoprint/templates/snippets/settings/printerprofiles/profileEditorBuildvolume.jinja2 +++ b/src/octoprint/templates/snippets/settings/printerprofiles/profileEditorBuildvolume.jinja2 @@ -41,7 +41,7 @@