Comments (14)
Great! I was actually going to open this exact issue yesterday, but got busy. I've been having this draft for a while:
all
optional
(Meansundefined
is allowed) (Note: I don't think we should allownull
, to align with default parameters)not
(Inverts the following predicates)not(type)
(Inverts just the argument) (Example:ow.string.not(ow.empty)
)any
(For multiple predicates, for example, when something can be both a string and array)is(function)
(Accepts a function that receives the value fromow
and is expected to return a boolean)
string
length(number)
minLength(number)
maxLength(number)
match(regex: RegExp)
(Seems likematches
would better align with other names likeincludes
,startsWidth
?)startsWith(string)
endsWith(string)
includes(string)
empty
(Only useful when used with.not
)nonEmpty
equal(string)
alphanumeric
numeric
date
number
inRange(start: number, end: number)
integer
equal(number)
positive
negative
finite
min(number)
max(number)
boolean
true
false
array
ofType(type, …)
(Accepts anyis.x
type) (TODO: Would be useful if this could also support anyow
predicate for each element, so you could do:ow.array.ofType(ow.string.minLength(5)))
length(number)
minLength(number)
maxLength(number)
startsWith(string)
endsWith(string)
includes(string, …)
includesAny(string, …)
empty
(Only useful when used with.not
)nonEmpty
deepEqual(array)
object
valuesOfType(type, …)
(Accepts anyis.x
type)plain
empty
(Only useful when used with.not
)nonEmpty
deepEqual(object)
instanceOf(object)
hasKeys(property, …)
hasAnyKeys(property, …)
date
before(date)
after(date)
error
evalError
rangeError
referenceError
syntaxError
typeError
uriError
name(string)
message(string)
messageIncludes(string)
map
size(number)
minSize(number)
maxSize(number)
keysOfType(type, …)
valuesOfType(type, …)
hasKeys(string, …)
hasValues(string, …)
hasAnyKeys(string, …)
hasAnyValues(string, …)
empty
(Only useful when used with.not
)nonEmpty
deepEqual(map)
set
size(number)
minSize(number)
maxSize(number)
ofType(type, …)
has(string, …)
hasAny(string, …)
empty
(Only useful when used with.not
)nonEmpty
deepEqual(set)
weakMap
keysOfType(type, …)
hasKeys(string, …)
hasAnyKeys(string, …)
weakSet
ofType(type, …)
has(string, …)
hasAny(string, …)
undefined
null
symbol
function
buffer
regExp
promise
typedArray
int8Array(value)
uint8Array(value)
uint8ClampedArray(value)
int16Array(value)
uint16Array(value)
int32Array(value)
uint32Array(value)
float32Array(value)
float64Array(value)
arrayBuffer(value)
sharedArrayBuffer(value)
dataView(value)
nan(value)
nullOrUndefined(value)
iterable(value)
gt(value: number)
lt(value: number)
I think min
and max
is clearer and more readable. What do you think?
float (number-is-float)
I don't really see the point of float
. 1
could be a float too. I don't see the value of differentiating it. integer
, however, could be useful, for when you only accept whole numbers.
negativeZero (negative-zero)
positiveZero (positive-zero)
I've honestly never needed this. Let's skip it until there are actual valid needs.
from ow.
I don't really see the point of float
Never needed it. Was just going through @kevva and your packages to see what we could use :).
I've honestly never needed this. Let's skip it until there are actual valid needs.
👌 Me neither.
I think
min
andmax
is clearer and more readable.
I'm fine with both. I might have a slight preference for gt
and lt
because I feel it reads easier.
ow(5, ow.number.gt(4));
//=> 5 should be a number greater than 4
ow(5, ow.number.min(4));
//=> 5 should be a number with a minimum of 4
One thing I thought about as well is to have an is
predicate on every type which accepts a function. This could keep ow
lighter with less built-in dependencies.
const ow = require('ow');
const inRange = require('in-range');
const validDate = require('vali-date');
const positive = require('is-positive');
ow(5, ow.number.is(positive).is(x => inRange(x, 0, 10));
ow('2017-05-21', ow.string.is(validDate));
It's just an example though, I think having things like positive
and inRange
in ow
itself would be very nice. The is
method could be just an extra which allows people to easily extend it.
from ow.
One thing I thought about as well is to have an is predicate on every type which accepts a function.
Good idea! I agree it would be useful. I've added it to the proposal.
from ow.
I'm fine with both. I might have a slight preference for gt and lt because I feel it reads easier.
I agree. It shows the intent better, and ow
is all about adding readability. I think it should be greaterThan
instead of gt
though.
from ow.
Thinking about it, I believe min
and greaterThan
are not identical.
min
: value >= x
greaterThan
: value > x
The intent of greaterThan
might be clearer, because with min
, users might not be sure if x
is included or excluded. But don't think it justifies that we should implement both.
from ow.
Thinking about it, I believe min and greaterThan are not identical.
I never implied they were.
But don't think it justifies that we should implement both.
I think it does. There's a reason we have both >
and >=
in most programming languages. It also makes the error message better.
from ow.
But maybe min
should be greaterThanOrEqual
? For consistency? I dunno.
from ow.
So we should add greaterThan
and greaterThanOrEqual
right?
from ow.
That's my suggestion, yes. The latter is a bit verbose but good for readability, and better than min
for consistency.
from ow.
Good idea! Added!
from ow.
We're almost done with this list, so I was looking into the ones that we didn't implement yet. I have a question regarding optional
optional
(Means undefined is allowed) (Note: I don't think we should allow null, to align with default parameters)
Should it be
ow(x, ow.optional.string.minLength(4));
// or
ow(x, ow.string.optional.minLength(4));
Or both of course.
If we go for either 2 or 3 (both), this mean we could write something like ow.string.minLength(4).optional.maxLength(5)
which doesn't really make sense. That's why I'm more in favour of 1.
from ow.
Lets go with 1
from ow.
notType(type) (Inverts just the argument) (Example: ow.string.notType(ow.empty))
I think it's better to just do #71
from ow.
Closing as we've done all of these. Wow!
from ow.
Related Issues (20)
- Bug: `isValid` wrong typing HOT 3
- Package does not seem to work on node v8 `SyntaxError: Unexpected token {` HOT 1
- string.nonBlank validator HOT 1
- Error running with metro bundler
- generate objects from predicate HOT 3
- use browser-side HOT 1
- Unclear error message when checking NaN against the number type HOT 7
- Composition HOT 3
- Is there a way for an object predicate to differentiate between key with value `undefined`, and key not present? HOT 2
- should `ow.string.equals` be narrowing the type to specific strings? HOT 1
- wrong message: Expected `e` to be of type .... - instead of Expected argument to be of type HOT 3
- Show all possible errors exhaustively (option?)
- const values HOT 1
- Q: how to make custom type narrowing validator (i.e. Predicate<x>)? HOT 3
- metadata
- Documentation is 404
- Build errors when using ow in React app compiled with vite
- Upgrading to 1.1.0 from older version makes TS complain HOT 6
- correct pattern for optional 'required' properties?
- Cannot find module 'ow' or its corresponding type declarations HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ow.