commit e74b675b3a23467a3be9c4debbc73df3dfd583e7 Author: Sven Slootweg Date: Sat May 16 00:33:43 2020 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/example.js b/example.js new file mode 100644 index 0000000..b62e4d9 --- /dev/null +++ b/example.js @@ -0,0 +1,22 @@ +"use strict"; + +const validationResult = require("./"); +const ValidationError = require("@validatem/error"); +const { validateValue } = require("@validatem/core"); /* FIXME: Add to devDeps */ + +function nonsenseValidator(value) { + return validationResult({ + errors: [ + new ValidationError("Some nonsense"), + new ValidationError("Some more nonsense"), + ], + newValue: value * 2 /* This will currently get lost into the void, because there are validation errors - a future version of Validatem may expose this "partial result", though */ + }); +} + +let result = validateValue(21, nonsenseValidator); +/* +AggregrateValidationError: One or more validation errors occurred: + - At (root): Some nonsense + - At (root): Some more nonsense +*/ diff --git a/index.js b/index.js new file mode 100644 index 0000000..82884eb --- /dev/null +++ b/index.js @@ -0,0 +1,15 @@ +"use strict"; + +const defaultValue = require("default-value"); + +module.exports = function validationResult({ errors, newValue }) { + return { + errors: defaultValue(errors, []), + newValue: newValue, + ___validatem_isSpecial: true, + ___validatem_isValidationResult: true, + ___validatem_validationResultVersion: 1, + }; +}; + +module.exports.__modulePath = __dirname; diff --git a/package.json b/package.json new file mode 100644 index 0000000..80162de --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "@validatem/validation-result", + "description": "Utility for creating a 'validation result', ie. an object that represents zero or more errors and an optional result value", + "version": "0.1.0", + "main": "index.js", + "repository": "http://git.cryto.net/validatem/validation-result.git", + "author": "Sven Slootweg ", + "license": "WTFPL OR CC0-1.0", + "dependencies": { + "default-value": "^1.0.0" + }, + "devDependencies": { + "@validatem/error": "^1.0.0" + } +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..fc277a4 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,27 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@validatem/error@^1.0.0": + version "1.0.0" + resolved "https://registry.yarnpkg.com/@validatem/error/-/error-1.0.0.tgz#a975904aa4c3e7618d89088a393567a5e1778340" + integrity sha512-7M3tV4DhCuimuCRdC2L/topBByDjhzspzeQGNU0S4/mdn2aDNtESYE43K/2Kh/utCAhqXh2gyw89WYxy//t3fQ== + dependencies: + create-error "^0.3.1" + +create-error@^0.3.1: + version "0.3.1" + resolved "https://registry.yarnpkg.com/create-error/-/create-error-0.3.1.tgz#69810245a629e654432bf04377360003a5351a23" + integrity sha1-aYECRaYp5lRDK/BDdzYAA6U1GiM= + +default-value@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/default-value/-/default-value-1.0.0.tgz#8c6f52a5a1193fe78fdc9f86eb71d16c9757c83a" + integrity sha1-jG9SpaEZP+eP3J+G63HRbJdXyDo= + dependencies: + es6-promise-try "0.0.1" + +es6-promise-try@0.0.1: + version "0.0.1" + resolved "https://registry.yarnpkg.com/es6-promise-try/-/es6-promise-try-0.0.1.tgz#10f140dad27459cef949973e5d21a087f7274b20" + integrity sha1-EPFA2tJ0Wc75SZc+XSGgh/cnSyA=