Browse Source

Initial commit

master
Sven Slootweg 1 year ago
commit
cadc3b46d3
  1. 1
      .gitignore
  2. 21
      README.md
  3. 17
      example.js
  4. 12
      index.js
  5. 12
      package.json
  6. 28
      yarn.lock

1
.gitignore

@ -0,0 +1 @@
node_modules

21
README.md

@ -0,0 +1,21 @@
# @validatem/combinator
Documentation for this module has not been fully written yet. By the time it reaches 1.0.0, it will have full documentation.
In the meantime, check out the `example.js` file in the repository for a usage demonstration.
A partial draft for the future documentation is below:
----
Used for specifying Validatem 'combinators', ie. validators that need to manually control what rules to apply to what (sub-)values. Typically used for validating data structures which have multiple internal values to validate, like in the [`arrayOf(...)`](https://www.npmjs.com/package/@validatem/array-of) combinator, or when validation should be conditional, like in the [`when(...)`](https://www.npmjs.com/package/@validatem/when) and [`either(...)`](https://www.npmjs.com/package/@validatem/either) combinators.
Note that this package doesn't actually really *do* anything. It just produces a special versioned object type, that's recognized (and handled accordingly) by the Validatem core; which will 'inject' the `applyValidators` function into your combinator, which you can use to apply a set of validation rules to a value.
This slightly odd dependency-injection design exists because `applyValidators` is tightly interwoven with the rest of the Validatem core, and it's not really possible to extract it out into a separate package without basically moving all of the core into it, defeating the point of packaging this separately.
## Why is this a separate package?
One of the design goals of Validatem is to minimize how much complexity and code you're adding to your project. Because of that, all of the plumbing used by validators and combinators is packaged as granularly as possible. This prevents the situation where you have 20 copies of the entire Validatem core floating around in your project, just because different validators happen to depend on slightly different versions.
By having several small 'plumbing' packages, validators *never* need to depend on `@validatem/core`, but only on the specific bits of plumbing that they need.

17
example.js

@ -0,0 +1,17 @@
"use strict";
const combinator = require("./");
const { validateValue } = require("@validatem/core"); /* FIXME: Add this as a devDep */
const isString = require("@validatem/is-string");
function createPassthroughCombinator(rules) {
// This is a dummy combinator that just applies the rules to the value (with the given context) as-normal. You could add any custom logic in here on when/how to apply validators, how to interpret the input rules/arguments of the factory function (if any), how to post-process the errors, and so on. You may want to look at some real combinators (`has-shape`, `array-of`, `dynamic`, `when`, etc.) to see some more realistic examples of how to effectively use this API.
return combinator((value, applyValidators, context) => {
return applyValidators(value, rules, context);
});
};
let result = validateValue("fourty-two", createPassthroughCombinator([ isString ]));
console.log(result); // fourty-two

12
index.js

@ -0,0 +1,12 @@
"use strict";
module.exports = function defineCombinator(callback) {
return {
callback: callback,
___validatem_isSpecial: true,
___validatem_isCombinator: true,
___validatem_combinatorVersion: 1
};
};
module.exports.__modulePath = __dirname;

12
package.json

@ -0,0 +1,12 @@
{
"name": "@validatem/combinator",
"description": "API for defining Validatem combinators",
"version": "0.1.0",
"main": "index.js",
"repository": "http://git.cryto.net/validatem/combinator.git",
"author": "Sven Slootweg <admin@cryto.net>",
"license": "WTFPL OR CC0-1.0",
"devDependencies": {
"@validatem/is-string": "^0.1.0"
}
}

28
yarn.lock

@ -0,0 +1,28 @@
# 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"
"@validatem/is-string@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@validatem/is-string/-/is-string-0.1.0.tgz#0ef6bc7a0111dafa9da277c7316c18d35c295f69"
integrity sha512-DoTf4FFgVjX8Bup5qLUIxTMqnf+NQOSPicHLfMsIQMx63+RHUe1JS2f1ih6kKTo/2nqLQndO0ir4FTghqgJFdw==
dependencies:
"@validatem/error" "^1.0.0"
is-string "^1.0.5"
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=
is-string@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/is-string/-/is-string-1.0.5.tgz#40493ed198ef3ff477b8c7f92f644ec82a5cd3a6"
integrity sha512-buY6VNRjhQMiF1qWDouloZlQbRhDPCebwxSjxMjxgemYT46YMd2NR0/H+fBhEfWX4A/w9TBJ+ol+okqJKFE6vQ==
Loading…
Cancel
Save