Skip to content

Commit

Permalink
Prepare for open sourcing (#22)
Browse files Browse the repository at this point in the history
* Rename and add LICENSE

* Build test and src separately

* Prepublish -> prepublishOnly

* Fix linting issues

* Fix tests

* Capitalise validVATNumber

* Add list of bundled validators to readme

* Reset version to 1.0.0

* Rename to `not-valid`

* Use createRegexValidator for sort code
  • Loading branch information
Jameskmonger authored Sep 11, 2018
1 parent a523009 commit 93989c5
Show file tree
Hide file tree
Showing 30 changed files with 256 additions and 216 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/bin/
/bin-test/
/node_modules/
*.js
*.d.ts
neworbit-validation-*.tgz
neworbit-validation-*.tgz
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2018 NewOrbit Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 changes: 35 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# @neworbit/validation
# not-valid

Composable message-based validation

## Installation

$ npm install @neworbit/validation --save
$ npm install not-valid --save

## Usage

### Overview

```typescript
import { validate } from "@neworbit/validation";
import { validate } from "not-valid";

// Use createValidator to specify a rule and the message given for breaking that rule
const mustContainA = createValidator<string>(v => v.indexOf("A") !== -1, "Value must contain the letter 'a'");
Expand All @@ -33,14 +35,41 @@ validate([
], "Too long a string, they say!"); // [ "Value must contain 'Z'", "Value must have length between 2 and 6" ]
```

### Validators

A number of validation functions come bundled with this package. You can use them like so:

```typescript
import { validators } from "not-valid";

validate([
validators.validLength({ min: 6, max: 12 })
], "Good value");
```

The validators included are as follows:

- `requiredString`
- `requiredNumber`
- `validLength`
- `validEmail`
- `validAlphaNumeric`
- `validOption`
- `validPhoneNumber`
- `validNINumber`
- `validUKDrivingLicence`
- `validSortCode`
- `validBankAccountNumber`
- `validVATNumber`

### Creating validation functions

A validation function must take in a value `value`, and return `Result.Pass` if `value` is valid, or `Result.Fail(message)` is `value` is invalid. They can also return `Result.Stop`, which will silently stop the validation cycle (no more errors).

This can be done with the helper method `createValidator` in `@neworbit/validation`:
This can be done with the helper method `createValidator` in `not-valid`:

```typescript
import { createValidator } from "@neworbit/validation";
import { createValidator } from "not-valid";

const mustContainA = createValidator<string>(v => v.indexOf("A") !== -1, "Value must contain the letter 'a'");
```
Expand Down Expand Up @@ -78,4 +107,4 @@ The validation will break on the first error, therefore only returning a single
validate([ something, another ], 5, { sequential: false });
```

If `something` fails validation, `another` will not be called.
If `something` fails validation, `another` will not be called.
Loading

0 comments on commit 93989c5

Please sign in to comment.