Neat module! I extended the module with a basic ow.number
and a positive
validator on the number. Works great and very easy and clean to read!
Discussed in Hangouts.
I think it's very nice. The thing I would like though is that the module comes with typings out of the box. The reason is code completion. I was writing a small type definition for the methods that you have right now, and it really helps writing code. You/we could even explain what the method means. Would totally love that. Very easy to explore all the different options without even looking at the documentation.
Here's a first draft of the type definitions.
interface Validator { }
interface StringValidator extends Validator {
/**
* Value should at least have a length of `length` characters.
*
* @param length The minimum length of the input.
*/
minLength(length: number): StringValidator;
/**
* Value should only have alphanumeric characters.
*/
alphanumeric: StringValidator;
}
interface NumberValidator extends Validator {
/**
* Value should be positive
*/
positive: NumberValidator;
}
interface Ow {
(value: any, predicate: Validator);
/**
* Value should be of type `string`
*/
string: StringValidator
/**
* Value should be of type `number`
*/
number: NumberValidator;
}
const ow: Ow;
export = ow;
Currently, I used my own type-assert module to check for things (not in my modules but for things I do at my work). It's just a wrapper around the type-check module.
I would totally start using this one as I like the API much more, and because you wrote the core ;). And also because this one will have more possibilities out of the box. One thing I miss very much in my module is the following.
const obj = {};
assert(obj.foo).is('String');
// TypeError: undefined is not of type String
ow(obj.foo, ow.string);
// ArgumentError: Expected argument to be of type `string` but received type `null`
I am not sure if possible though, but it would be nice if somehow, we could show the error message like this
ArgumentError: Expected argument
obj.foo
to be of typestring
but received typenull
This might be way out of scope, but when typechecking a lot of things one after the other, this could be a very nice feature. Like I said before, just throwing in some ideas :).
There's also an issue for this in type-assert
(SamVerschueren/type-assert#3).
There will always be stuff that Ow doesn't cover. For instance, checking if a string is a valid date. It would be nice if someone could hook his own method/module into the Ow DSL.
ow.string.register('date', input => /^\d{4}-\d{2}-\d{2}$/.test(input));
ow('2017-09-12', ow.string.date);
Or if I want to publish all kind of date checks for Ow, I could create a module ow-date
and just require it.
require('ow-date');
ow('2017-09-12', ow.string.date.after('2018-01-01'));
You can think of it like with RxJS. The core is pretty small, but it let's you add more operators with a simple require
/import
.