# validate-for

> A simple library that uses the builder pattern to validate values and objects in js

Latest version **1.0.9** (published 2020-03-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install validate-for
pnpm add validate-for
yarn add validate-for
bun add validate-for
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.9 |
| Published | 2020-03-06 |
| First published | 2020-02-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 11 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | rahicks26 |
| Maintainers | rahicks26 |
| Keywords | validation, builder-pattern |

## Links

- npm: https://www.npmjs.com/package/validate-for
- npm.io page: https://npm.io/package/validate-for

## Dependencies (3)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [immutable](https://npm.io/package/immutable.md) ^4.0.0-rc.12
- [get-prop-by-name](https://npm.io/package/get-prop-by-name.md) ^1.0.1

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.9 (latest) — 2020-03-06
- 1.0.8 — 2020-03-06
- 1.0.7 — 2020-02-19
- 1.0.6 — 2020-02-19
- 1.0.5 — 2020-02-16
- 1.0.3 — 2020-02-16
- 1.0.2 — 2020-02-14
- 1.0.1 — 2020-02-14
- 1.0.0 — 2020-02-14

## README

# Install 

TODO....

# Use
Validate-this leverages the builder pattern to handle validation
for both entire objects and single values. At the heart of this 
project is the rule builder class. To create a new rule use the 
rule method.

```javascript
const myRule = rule();
```

Once you have an instance of a ruleBuilder you will be able to 
use its builder methods to build out custom validation rules. 
Such as:

```javascript
const myRuleWithConstraint = myRule.must(..)
const myRuleWithContext = myRule.withContext(..)
const myRuleWithDisplayName = myRule.withDisplayName(..)
const myRuleWithGetter = myRule.withGetter(..)
```

Each of these methods will return a new instance of the rule
with the requested method applied to only the new instance.
This is to make it easier to chain rule together like so:

```javascript
const ctx = {prop:'I am hungry'}
const myRule = rule()
  .must((value)=> !!value)  //Make sure the value is truthy
  .must((value,ctx)=> value !== ctx.prop) // Make sure the value is not equal prop
  .withContext(ctx,true) // Sets the context and makes sure the reference to the original model is preserved

ctx.prop2 = 'I am Full'

const myRule2 = myRule
  .withContext(ctx) // Sets the context to be cloned each time this will preserve the state of the context when withContext was called
  .must((value, ctx)=> value !=== ctx.prop2 )

```

Building rules like this can make it easy to share common 
configuration such as all fields are required.


Now to evaluate a rule you have 3 helper functions

```javascript
const myRule = rule.must(v=>v)

// To evaluate against a value all you have to do is pass the value,
// unless the rule needs a context to run.
// If a context is required then an error will be returned with
// cannotEvaluate flag set to true.  
const resultOfSimpleValueCheck = myRule.evaluate(1);

//To evaluate against a model you must have a getter set. Otherwise undefined will be returned
const resultOfValidationForAContext = myRule.withGetter('count').evaluateFor({count:2}) 

const func = myRule
  .must((_,ctx) => ctx.prop.innerProp.contains('I eat squid for breakfast'))
  .withGetter(ctx=>prop.innerProp)
  .withContext({prop:{innerProp:'Not today!'}})
  .asFunc();

// returns {name:null, error:'Property is Invalid', isInvalid:true}
const result = func(1)
```

In addition to the ruleBuilder class

---
_Source: https://npm.io/package/validate-for · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
