Skip to content

Commit

Permalink
NPM Publishing Cleanup (#6)
Browse files Browse the repository at this point in the history
* Removes unnecessary files from npm
* Updates README with more examples, and a TOC
  • Loading branch information
losandes authored May 12, 2019
1 parent d0f9210 commit 0b294cb
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 98 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.DS_Store

# Test Output
test-ts.js
examples-typescript.js

# Logs
logs
Expand Down
12 changes: 12 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.DS_Store
.gitignore
.eslintrc
npm-debug.log
build.js
examples-typescript.js
examples-typescript.ts
index.browser.js
pre-push.js
test.js
*.test.js
tsconfig.json
64 changes: 52 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
@polyn/blueprint
================
@polyn/blueprint is an easy to use, flexible, and powerful validation library for nodejs and browsers
@polyn/blueprint is an easy to use, extensible, and powerful validation library for nodejs and browsers.

While @polyn/blueprint can be used on it's own, it really shines when you use it with [@polyn/immutable](https://github.com/losandes/polyn-immutable).

* [Getting Started with Node](#node)
* [Getting Started with the Browser](#browser)
* [Types / Validators](#types-validators)
* [Custom Validators](#custom-validators)
* [Inline Custom Validators](#inline-custom-validators)
* [Registering Validators](#registering-validators)
* [Registering Types](#registering-types)
* [Registering Regular Expressions](#registering-regular-expressions)
* [Registering Blueprints](#registering-blueprints)
* [Intercepting Values](#intercepting-values)
* [TypeScript Support](#typescript-support)

## Usage

Expand Down Expand Up @@ -171,11 +185,11 @@ const allTheTypes = blueprint('allTheTypes', {
## Custom Validators
Blueprint supports multiple ways for defining your own validators.
* Inline custom validators
* Registered validators
* Registered types
* Registered expressions
* Registered blueprints
* [Inline Custom Validators](#inline-custom-validators)
* [Registered validators](#registering-validators)
* [Registered types](#registering-types)
* [Registered expressions](#registering-regular-expressions)
* [Registered blueprints](#registering-blueprints)
### Custom Validator Return Values
_Inline custom validators_, _registered validators_, and _registered types_ support passing your own function to perform the validation. Your functions must return one of two types to work. The following examples will use inline custom validators for the example, but the same approach can be used when registering validators, or types.
Expand Down Expand Up @@ -250,6 +264,7 @@ const product = blueprint('product', {
// require the ISBN if the productType is 'book'
ISBN: ({ key, value, input, root }) =>
root.productType === 'book' && typeof value === 'string' && value.length
})
```
> @alsosee [Custom Validator Return Values](#custom-validator-return-values) for information on supported return values
Expand Down Expand Up @@ -280,7 +295,7 @@ assert.strictEqual(actual.value.age, 20)
> @alsosee [Custom Validator Return Values](#custom-validator-return-values) for information on supported return values
#### Registering Types
### Registering Types
If you want to support nulls and arrays for the validator you are registering, use `registerTypes` instead. For instance, to register the following:
```
Expand Down Expand Up @@ -309,7 +324,7 @@ const actual = blueprint('requestBody', {
> @alsosee [Custom Validator Return Values](#custom-validator-return-values) for information on supported return values
#### Registering Regular Expressions
### Registering Regular Expressions
Registering regular expressions works similarly to registering types, except you pass in a RegExp, or string expression as the second argument.
```JavaScript
Expand All @@ -331,7 +346,7 @@ const actual = blueprint('requestBody', {
})
```
#### Registering Blueprints
### Registering Blueprints
Sometimes it's useful to register another blueprint as a validator. `registerBlueprint` accepts the same arguments as constructing one, and stores the blueprint as a validator, so it can be used like a type. It includes support for nulls and arrays.
```JavaScript
Expand Down Expand Up @@ -373,7 +388,7 @@ assert.strictEqual(actual.value.person.lastName, 'Doe')
// ...
```
#### Intercepting Values
### Intercepting Values
_Inline custom validators_, _registered validators_, and _registered types_ provide the ability to intercept/modify the values that are returned by blueprint.validate.
Both of these examples trim the strings that are written to the `value` property
Expand Down Expand Up @@ -428,7 +443,32 @@ console.log(
// }
```
## TypeScript Support
This library exports types. A brief example is shown here. If you'd like to see more, there are several examples in [examples-typescript.ts](./examples-typescript.ts).
```TypeScript
const { blueprint, gt } = require('@polyn/blueprint')

const personBlueprint: IBlueprint = blueprint('Person', {
firstName: 'string',
lastName: 'string',
age: gt(0)
})

const result: IValueOrError = personBlueprint.validate({
firstName: 'John',
lastName: 'Doe',
age: 21
})

console.log(result.err)
// prints null

console.log(result.value)
// prints { firstName: 'John', lastName: 'Doe', age: 21 }
```
## Why @polyn/blueprint? Why not JSON Schema
There's noting wrong with JSON Schema. In many cases it makes more sense to use that, particularly in cases where the schema is meant to be shared across organizational boundaries. I started writing this library years ago when JSON Schema was in it's infancy. So why keep improving and maintaining it?
There's nothing wrong with JSON Schema. In many cases it makes more sense to use that, particularly in cases where the schema is meant to be shared across organizational boundaries. I started writing this library years ago when JSON Schema was in it's infancy. So why keep improving and maintaining it?
It's simple. It's less verbose than JSON Schema. It's functional (we write validators as functions, instead of configurations). Because of that it's easily extensible, and it's easy to write complex/dependency-based validations (i.e. _isbn is required if productType === 'book'_).
It's simple. It's less verbose than JSON Schema. It's functional (we can write validators as functions, instead of configurations). Because of that it's easily extensible, and it's easy to write complex/dependency-based validations (i.e. _isbn is required if productType === 'book'_).
2 changes: 1 addition & 1 deletion test-ts.ts → examples-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ const result: IValueOrError = myModelBp.validate({
})

expect(result.err).to.be.null
console.log('ALL TYPESCRIPT TESTS PASSED')
console.log('ALL TYPESCRIPT EXAMPLES PASSED\n')
84 changes: 10 additions & 74 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
{
"name": "@polyn/blueprint",
"version": "2.0.2",
"version": "2.0.4",
"description": "An easy to use, flexible, and powerful validation library for nodejs and browsers",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"build": "node build.js",
"pre-push": "npm test && node pre-push.js",
"test": "node test.js && npm run test:ts",
"test:ts": "tsc -p ./tsconfig.json && node test-ts.js",
"test:ts": "tsc -p ./tsconfig.json && node examples-typescript.js",
"watch": "nodemon -e js --exec 'node test.js --tap | tap-nyan'"
},
"pre-commit": [
"build"
],
"pre-push": [
"test"
],
"pre-push": ["pre-push"],
"repository": {
"type": "git",
"url": "git+https://github.com/losandes/polyn-blueprint.git"
Expand Down Expand Up @@ -45,9 +41,9 @@
"eslint-plugin-promise": "~4.1.1",
"eslint-plugin-standard": "~4.0.0",
"nodemon": "~1.19.0",
"pre-commit": "~1.2.2",
"pre-push": "~0.1.1",
"supposed": "~0.2.0",
"terminal-adventure": "~1.0.2",
"typescript": "~3.4.5"
},
"dependencies": {}
Expand Down
3 changes: 3 additions & 0 deletions pre-push.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const FORMAT = '\x1b[33m\x1b[5m%s\x1b[0m'
console.log(FORMAT, 'Did you build the browser distribution?')
console.log(FORMAT, 'Did you update the package number?\n')
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"sourceMap": false,
"strict": true
},
"include": ["./test-ts.ts"],
"include": ["examples-typescript.ts"],
"exclude": []
}

0 comments on commit 0b294cb

Please sign in to comment.