mirror of https://github.com/lukechilds/ow.git
Sindre Sorhus
7 years ago
commit
bd362ece97
13 changed files with 240 additions and 0 deletions
@ -0,0 +1,12 @@ |
|||
root = true |
|||
|
|||
[*] |
|||
indent_style = tab |
|||
end_of_line = lf |
|||
charset = utf-8 |
|||
trim_trailing_whitespace = true |
|||
insert_final_newline = true |
|||
|
|||
[*.yml] |
|||
indent_style = space |
|||
indent_size = 2 |
@ -0,0 +1,2 @@ |
|||
* text=auto |
|||
*.js text eol=lf |
@ -0,0 +1,2 @@ |
|||
node_modules |
|||
yarn.lock |
@ -0,0 +1 @@ |
|||
package-lock=false |
@ -0,0 +1,5 @@ |
|||
language: node_js |
|||
node_js: |
|||
- '8' |
|||
- '6' |
|||
- '4' |
@ -0,0 +1,18 @@ |
|||
'use strict'; |
|||
const ow = require('.'); |
|||
|
|||
const fn = (input, options) => { |
|||
ow(input, ow.string.minLength(10)); |
|||
|
|||
// For objects, just an idea for now:
|
|||
// ow.many(options, {
|
|||
// tasks: ow.number.range(0, 10),
|
|||
// extras: ow.arrayOf(ow.number)
|
|||
// });
|
|||
}; |
|||
|
|||
//fn(10);
|
|||
//=> ArgumentError: Expected argument to be of type `string` but received type `number`
|
|||
|
|||
fn('foo'); |
|||
//=> ArgumentError: Expected string length to be minimum 10
|
After Width: | Height: | Size: 466 KiB |
@ -0,0 +1,66 @@ |
|||
'use strict'; |
|||
const is = require('@sindresorhus/is'); |
|||
|
|||
class ArgumentError extends Error { |
|||
constructor(message, context) { |
|||
super(message); |
|||
// TODO: Node does not preserve the error name in output when using the below, why?
|
|||
//Error.captureStackTrace(this, context);
|
|||
this.name = 'ArgumentError'; |
|||
} |
|||
} |
|||
|
|||
const ow = (value, predicate) => { |
|||
for (const validator of predicate.context.validators) { |
|||
const result = validator(value); |
|||
if (result) { |
|||
// TODO: Modify the stack output to show the original `ow()` call instead of this `throw` statement
|
|||
throw new ArgumentError(result, ow); |
|||
} |
|||
} |
|||
}; |
|||
|
|||
const newInstance = (fn, context) => { |
|||
const instance = fn(context); |
|||
instance.context = context; |
|||
return instance; |
|||
}; |
|||
|
|||
const stringPredicates = context => ({ |
|||
minLength: number => { |
|||
context.validators.push(value => { |
|||
if (value.length < number) { |
|||
return `Expected string length to be minimum ${number}`; |
|||
} |
|||
}); |
|||
return newInstance(stringPredicates, context); |
|||
}, |
|||
get alphanumeric() { |
|||
context.validators.push(value => { |
|||
if (/[a-z\d]/gi.test(value)) { |
|||
return `Expected string to contain only alphanumeric characters but received \`${value}\``; |
|||
} |
|||
}); |
|||
return newInstance(stringPredicates, context); |
|||
} |
|||
}); |
|||
|
|||
Object.defineProperty(ow, 'string', { |
|||
enumerable: true, |
|||
get() { |
|||
const instance = newInstance(stringPredicates, { |
|||
validators: [] |
|||
}); |
|||
|
|||
instance.context.validators.push(value => { |
|||
// TODO: Create a generic function for all types that can be used here
|
|||
if (!is.string(value)) { |
|||
return `Expected argument to be of type \`string\` but received type \`${is(value)}\``; |
|||
} |
|||
}); |
|||
|
|||
return instance; |
|||
} |
|||
}); |
|||
|
|||
module.exports = ow; |
@ -0,0 +1,9 @@ |
|||
MIT License |
|||
|
|||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) |
|||
|
|||
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. |
@ -0,0 +1,49 @@ |
|||
{ |
|||
"name": "ow", |
|||
"version": "0.0.1", |
|||
"description": "Argument type validation", |
|||
"license": "MIT", |
|||
"repository": "sindresorhus/ow", |
|||
"author": { |
|||
"name": "Sindre Sorhus", |
|||
"email": "sindresorhus@gmail.com", |
|||
"url": "sindresorhus.com" |
|||
}, |
|||
"engines": { |
|||
"node": ">=4" |
|||
}, |
|||
"scripts": { |
|||
"test": "xo && ava" |
|||
}, |
|||
"files": [ |
|||
"index.js", |
|||
"example.js" |
|||
], |
|||
"keywords": [ |
|||
"type", |
|||
"types", |
|||
"check", |
|||
"checking", |
|||
"guard", |
|||
"guards", |
|||
"assert", |
|||
"assertion", |
|||
"predicate", |
|||
"predicates", |
|||
"is", |
|||
"validate", |
|||
"validation", |
|||
"utility", |
|||
"util", |
|||
"typeof", |
|||
"instanceof", |
|||
"object" |
|||
], |
|||
"dependencies": { |
|||
"@sindresorhus/is": "^0.2.0" |
|||
}, |
|||
"devDependencies": { |
|||
"ava": "*", |
|||
"xo": "*" |
|||
} |
|||
} |
@ -0,0 +1,36 @@ |
|||
# ow [![Build Status](https://travis-ci.org/sindresorhus/ow.svg?branch=master)](https://travis-ci.org/sindresorhus/ow) |
|||
|
|||
> Argument type validation |
|||
|
|||
<img src="header.gif" width="220" align="right"> |
|||
|
|||
|
|||
## Install |
|||
|
|||
``` |
|||
$ npm install ow |
|||
``` |
|||
|
|||
|
|||
## Usage |
|||
|
|||
```js |
|||
const ow = require('ow'); |
|||
|
|||
const unicorn = input => { |
|||
ow(input, ow.string.minLength(5)); |
|||
|
|||
… |
|||
); |
|||
|
|||
unicorn(3); |
|||
//=> ArgumentError: Expected argument to be of type `string` but received type `number` |
|||
|
|||
unicorn('yo'); |
|||
//=> ArgumentError: Expected string length to be minimum 10 |
|||
``` |
|||
|
|||
|
|||
## License |
|||
|
|||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
@ -0,0 +1,6 @@ |
|||
import test from 'ava'; |
|||
import m from '.'; |
|||
|
|||
test('', t => { |
|||
|
|||
}); |
@ -0,0 +1,34 @@ |
|||
# TODO |
|||
|
|||
- Write it in TypeScript so we get great docs. |
|||
|
|||
- Make it easy to extend and add custom predicates |
|||
|
|||
- Parse out the input string literal when possible to create good error messages: |
|||
|
|||
```js |
|||
// Before |
|||
ow(obj.foo, ow.string); |
|||
// ArgumentError: Expected argument to be of type `string` but received type `null` |
|||
|
|||
// After |
|||
ow(obj.foo, ow.string); |
|||
// ArgumentError: Expected argument `obj.foo` to be of type `string` but received type `null` |
|||
``` |
|||
|
|||
- Ability to reuse validations: |
|||
|
|||
```js |
|||
const checkUsername = ow.create(ow.string.minLength(4)); |
|||
checkUsername(username); |
|||
``` |
|||
|
|||
- At some point would be nice to parse the source on error and show the error pointing to the source. Using https://www.npmjs.com/package/babel-code-frame |
|||
|
|||
- Ability to use custom assertions. Should be very easy to create. We can't bundle it all. |
|||
|
|||
|
|||
--- |
|||
|
|||
|
|||
Feedback from Sam: https://gist.github.com/SamVerschueren/6e1c7e9f3183dbe1b7d8909c6ff07337 |
Loading…
Reference in new issue