antonioru / deep-waters Goto Github PK
View Code? Open in Web Editor NEW🔥Deep Waters is an easy-to-compose functional validation system for javascript developers 🔥
Home Page: https://antonioru.gitbook.io/deep-waters/
License: MIT License
🔥Deep Waters is an easy-to-compose functional validation system for javascript developers 🔥
Home Page: https://antonioru.gitbook.io/deep-waters/
License: MIT License
@antonioru I know your planning to tackle this this weekend. Couldn't get it off my mind and had some free time so heres a proof of concept script.
There's a lot of room for improvement and maybe some optimizations. Hope its helpful. Let me know what you think.
/**
* All around functional helpers
*/
const compose = (...funcs) => (...args) => funcs.slice(1).reduce((acc, fn) => fn(acc), funcs[0](...args))
const partial = (fn, ...args) => (...rest) => fn(...args, ...rest)
/**
* Function for converting validator function
* boolean results to an object
*/
const objectize = (doc, fn, value) => ({
...doc,
valid: fn(value),
value
})
/**
* When composing many validator function this will
* aggregate into passed and failed results
*/
const aggregateResults = (results) => ({
passed: results.filter(r => r.valid),
failed: results.filter(r => !r.valid)
})
/**
* Some more helpers for composing and/or validations
*/
const applyDoc = (doc) => (result) => ({ ...result, ...doc })
const applyValue = (value) => (result) => ({ ...result, value })
const runValidators = (validators) => (value) => validators.map(validator => validator(value))
/**
* Determines if a result of composed functions
* is valid or not
*/
const orValid = (result) => ({ ...result, valid: result.passed.length > 0 })
const andValid = (result) => ({ ...result, valid: result.failed.length === 0 })
/**
* HOF used to take simple bool validation functions and
* convert to full object style validators
*/
const validator = (fn) => (doc) => partial(objectize, doc, fn)
// const orValidator = (...validators) => (doc) => value => orValid(applyDoc(doc)(aggregateResults(runValidators(validators)(value))))
const orValidator = (...validators) => (doc) => (value) => compose(
runValidators(validators),
aggregateResults,
applyDoc(doc),
applyValue(value),
orValid)(value)
const andValidator = (...validators) => (doc) => (value) => compose(
runValidators(validators),
aggregateResults,
applyDoc(doc),
applyValue(value),
andValid)(value)
/**
* Same as before - bool validator functions. Some of these
* abbreviated for POC sake. Validators that were previously
* composed at this level would now be composed further
* below as validators
*/
const ofClass = (name, value) => Object.prototype.toString.call(value) === `[object ${name}]`
const isFalse = value => value === false
const isZero = value => value === 0
const isNumber = partial(ofClass, 'Number')
const biggerThan = number => value => value > number
const smallerThan = number => value => value < number
/**
* Object style validators composed with
* the core bool validators and descripting
* objects
*/
const isZeroValidator = validator(isZero)({
name: 'isZero'
})
const isFalseValidator = validator(isFalse)({
name: 'isFalse'
})
const isNumberValidator = validator(isNumber)({
name: 'isNumber'
})
const biggerThanValidator = (min) => validator(biggerThan(min))({
name: 'biggerThan',
expected: `number bigger than ${min}`
})
const smallerThanValidator = (max) => validator(smallerThan(max))({
name: 'smallerThan',
expected: `number smaller than ${max}`
})
const isFalsyValidator = orValidator(isZeroValidator, isFalseValidator)({
name: 'isFalsy'
})
const betweenValidator = (min) => (max) => andValidator(biggerThanValidator(min), smallerThanValidator(max))({
name: 'betweenValidator'
})
/**
*
* Some POC Tests :smile:
*
*/
const assert = (condition) => {
if (!condition) throw 'Assertion failed'
}
const isZeroResult = isZeroValidator(0)
console.log('isZeroResult ===>', isZeroResult)
assert(isZeroResult.name === 'isZero')
assert(isZeroResult.valid === true)
assert(isZeroResult.value === 0)
const isFalseResult = isFalseValidator(true)
console.log('isFalseResult ===>', isFalseResult)
assert(isFalseResult.name === 'isFalse')
assert(isFalseResult.valid === false)
assert(isFalseResult.value === true)
const biggerThanResult = biggerThanValidator(10)(50)
console.log('biggerThanResult ===>', biggerThanResult)
assert(biggerThanResult.name === 'biggerThan')
assert(biggerThanResult.valid === true)
assert(biggerThanResult.value === 50)
assert(biggerThanResult.expected === 'number bigger than 10')
const isFalsyResult = isFalsyValidator(0)
console.log('isFalsyResult ===>', isFalsyResult)
assert(isFalsyResult.name === 'isFalsy')
assert(isFalsyResult.valid === true)
assert(isFalsyResult.value === 0)
assert(isFalsyResult.passed[0].name === 'isZero')
const betweenResult = betweenValidator(10)(20)(9)
console.log('betweenResult ===>', betweenResult)
assert(betweenResult.name === 'betweenValidator')
assert(betweenResult.valid === false)
assert(betweenResult.value === 9)
assert(betweenResult.passed[0].name === 'smallerThan')
Also, wasn't sure how to share this with you... hope issue is ok 👍❓
Hi,
Thank you very much for your contribution! it is super awesome!
I'm still missing here some kind of message system, so we can know which validation rule has failed and show the correct message to the user.
Also, if result and pipe will be async we could also have async validation.
Do you have any ideas for these subjects and how to implement it with current state of the project?
Maybe implementing Future
type? and compose each validator with a Message
type?
Hi,
First of all, I want to congratulate the author because I believe this is a very promising project. And you've obviously put a lot of time into this. If there is any way I can help, please let me know.
Now, to the point: I've been looking into the code and I've found that the isURL function is not always working as expected. For example, the following code returns true:
const DW = require("deep-waters");
console.log(DW.isURL("www.google"));
As you mentioned in the comments of isURL.js, the code for this function is derived from this Stackoverflow question. The example I've used above was also already mentioned in the comments on Stackoverflow as an invalid example.
Now, the question "what is a valid URL?" is indeed quire ambiguous. But to me, "www.google" should return false for this function. In any case, it would be best to either include specific documentation/examples to explain what works and what not. Were you thinking about also adding more documentation to this project?
As a second measure, we can try to improve the regular expression so that it returns false for these type of cases.
What do you think?
Hi there,
I couldn't find a SECURITY.md
in your repository and am not sure how to best contact you privately to disclose a security issue.
Can you add a SECURITY.md
file with an e-mail to your repository, so that our system can send you the vulnerability details? GitHub suggests that a security policy is the best way to make sure security issues are responsibly disclosed.
Once you've done that, you should receive an e-mail within the next hour with more info.
Thanks! (cc @huntr-helper)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.