Skip to content

Commit

Permalink
Merge 1a0882a into cccc1e9
Browse files Browse the repository at this point in the history
  • Loading branch information
João Dias Conde Azevedo committed Apr 22, 2021
2 parents cccc1e9 + 1a0882a commit 41320c7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
13 changes: 13 additions & 0 deletions js/util/validation.js
Expand Up @@ -19,6 +19,19 @@ const EMAIL_REGEX = /^[\w\d\._%+-]+@[\w\d\.\-]+$/;
*/
const URL_REGEX = /^\w+\:\/\/([^@]+\:[^@]+@)?[^\:\/\?#]+(\:\d+)?(\/[^\?#]+)*\/?(\?[^#]*)?(#.*)?$/;

export const all = function(elementValidation, message = "Not all elements satisfy %{1}") {
const validation = (collection, ctx) => {
for (const element of collection) {
const valid = elementValidation(element);
if (!valid) {
throw new ValidationError(message.replace("%{1}", String(elementValidation.name)));
}
}
return true;
};
return validation;
};

export const eq = function(valueC, message = "Must be equal to %{1}") {
const validation = (value, ctx) => {
if (value === null) return true;
Expand Down
24 changes: 12 additions & 12 deletions package.json
Expand Up @@ -50,29 +50,29 @@
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@babel/core": "^7.13.10",
"@babel/preset-env": "^7.13.10",
"@babel/core": "^7.13.16",
"@babel/preset-env": "^7.13.15",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^17.1.0",
"@rollup/plugin-commonjs": "^18.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^11.2.0",
"@typescript-eslint/eslint-plugin": "^4.17.0",
"@typescript-eslint/parser": "^4.17.0",
"@rollup/plugin-node-resolve": "^11.2.1",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"coveralls": "^3.1.0",
"eslint": "^7.21.0",
"eslint": "^7.24.0",
"eslint-config-hive": "^0.5.3",
"fastify": "^3.14.0",
"fastify": "^3.15.0",
"mocha": "^8.3.2",
"mocha-cli": "^1.0.1",
"mongoose": "^5.12.0",
"npm-check-updates": "^11.2.2",
"mongoose": "^5.12.5",
"npm-check-updates": "^11.5.1",
"nyc": "15.1.0",
"prettier": "^2.2.1",
"prettier-config-hive": "^0.1.7",
"rollup": "^2.41.1",
"rollup": "^2.45.2",
"rollup-plugin-dts": "^3.0.1",
"rollup-plugin-node-polyfills": "^0.2.1",
"sort-package-json": "^1.49.0",
"typescript": "^4.2.3"
"typescript": "^4.2.4"
}
}
24 changes: 24 additions & 0 deletions test/util/validation.js
@@ -1,6 +1,30 @@
const assert = require("assert");
const yonius = require("../..");

describe("#all()", function() {
it("should verify a condition for all elements in a collection", () => {
let result;

result = yonius.all(yonius.isUrl())([
"https://www.platforme.com/",
"https://www.google.com/"
]);
assert.strictEqual(result, true);

result = yonius.all(yonius.eq(2))([2, 2, 2, 2]);
assert.strictEqual(result, true);

assert.throws(
() => yonius.all(yonius.isUpper())(["illegal!"]),
err => {
assert.strictEqual(err instanceof yonius.ValidationError, true);
assert.strictEqual(err.message, "Value contains lower cased characters");
return true;
}
);
});
});

describe("#eq()", function() {
it("should verify basic equal operations", () => {
let result;
Expand Down

0 comments on commit 41320c7

Please sign in to comment.