Initial commit

master
Sven Slootweg 4 years ago
commit f494bf527a

@ -0,0 +1,3 @@
{
"extends": "@joepie91/eslint-config"
}

1
.gitignore vendored

@ -0,0 +1 @@
node_modules

@ -0,0 +1,34 @@
# @validatem/normalize-rules
This library implements the logic that Validatem uses internally for flattening and normalizing a list of rules (validators and combinators), prior to applying them.
It's particularly useful if you're writing a combinator that does some sort of preprocessing on rules before applying them, and you need the same flattening/normalization logic that `@validatem/core` uses.
Please see the [Validatem website](https://validatem.cryto.net/) for full documentation on how this package fits into the ecosystem of utility and internals packages. This README only includes the API docunentation.
## 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.
## License, donations, and other boilerplate
Licensed under either the [WTFPL](http://www.wtfpl.net/txt/copying/) or [CC0](https://creativecommons.org/publicdomain/zero/1.0/), at your choice. In practice, that means it's more or less public domain, and you can do whatever you want with it. Giving credit is *not* required, but still very much appreciated! I'd love to [hear from you](mailto:admin@cryto.net) if `validatem` was useful to you.
Creating and maintaining open-source modules is a lot of work. A donation is also not required, but much appreciated! You can donate [here](http://cryto.net/~joepie91/donate.html).
If you are contributing to this project, keep in mind that you will also be making your contributions available under the above licenses.
## API
### normalizeRules(rules, [options])
Takes a list of rules and returns a single, normalized, flattened array of rules.
- __rules:__ The list of rules to normalize. Note that multiple formats are accepted:
- A single validator/combinator, without any array wrapping.
- A flat array of validators/combinators.
- An arbitrarily nested array of (arrays of arrays of ...) validators/combinators, which will be flattened.
- __options:__ *Optional.* An object of options.
- __normalizeObjects:__ Whether to turn object literals into `@validatem/has-shape` combinators, like Validatem does internally. If you want to pre-process object-literal rules, you probably want to keep this turned off. Defaults to `false`.

@ -0,0 +1,35 @@
"use strict";
const assureArray = require("assure-array");
const isPlainObj = require("is-plain-obj");
const defaultValue = require("default-value");
const flat = require("array.prototype.flat");
const matchSpecial = require("@validatem/match-special");
const isPlainObjectValidator = require("@validatem/is-plain-object");
const hasShape = require("@validatem/has-shape");
// FIXME: Write an example.js
module.exports = function normalizeRules(rules, options) {
let normalizeObjects = defaultValue(options.normalizeObjects, false);
// TODO: Switch to `Array#flat` once it is available
let flattened = flat(assureArray(rules));
let actualRules = flattened.filter((rule) => rule != null);
if (normalizeObjects) {
return actualRules.flatMap((rule) => {
if (isPlainObj(rule) && !matchSpecial(rule)) {
return [
isPlainObjectValidator,
hasShape(rule)
];
} else {
return rule;
}
});
} else {
return actualRules;
}
};

@ -0,0 +1,22 @@
{
"name": "@validatem/normalize-rules",
"description": "Flattens and normalizes a list of rules, ready for pre-processing in a combinator",
"version": "0.1.0",
"main": "index.js",
"repository": "http://git.cryto.net/validatem/normalize-rules.git",
"author": "Sven Slootweg <admin@cryto.net>",
"license": "WTFPL OR CC0-1.0",
"dependencies": {
"@validatem/has-shape": "^0.1.0",
"@validatem/is-plain-object": "^0.1.0",
"@validatem/match-special": "^0.1.0",
"array.prototype.flat": "^1.2.3",
"assure-array": "^1.0.0",
"default-value": "^1.0.0",
"is-plain-obj": "^2.1.0"
},
"devDependencies": {
"@joepie91/eslint-config": "^1.1.0",
"eslint": "^6.8.0"
}
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save