question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature idea: Using Joi for prop definition and validation, or adding Joi-like features

See original GitHub issue

Have you guys used Joi? I use Joi on the server-side with Hapi and really love it.

I just got started with Ampersand and also really, really love ampersand-state. What a great tool. I was just in the middle of defining some properties on a view (!!!) and wanted to define valid values for one properties a function of the value of another property. Joi lets you do this with alternatives and references.

If Joi was adopted for properties, it could then also be used for validation. It would be a great win, I think. Defining a properties schema already gets you quite close to using that schema for validation.

This wouldn’t be trivial, of course. But I wanted to get this out there to see what you think. Joi is apparently browser-compatible via browserify, though they don’t run browser tests and they support things like Buffers. So, Joi may be too heavy for this purpose, and if so, perhaps just the above-mentioned reference/alternatives features could be added. And validation via properties’ schemas, I think, would be great too.

Thank you.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
wilmoorecommented, Jun 10, 2016

This would be doable as a dataType mixin with a minor non-breaking change to dataType.set and def.test.

original

            // check type if we have one
            if (dataType && dataType.set) {
                cast = dataType.set(newVal);
                newVal = cast.val;
                newType = cast.type;
            }

            // If we've defined a test, run it
            if (def.test) {
                err = def.test.call(this, newVal, newType);
                if (err) {
                    throw new TypeError('Property \'' + attr + '\' failed validation with error: ' + err);
                }
            }

becomes

            // check type if we have one
            if (dataType && dataType.set) {
                cast = dataType.set(newVal, attr, def); // << adds `attr` and `def` to `.set` call
                newVal = cast.val;
                newType = cast.type;
            }

            // If we've defined a test, run it
            if (typeof def.test === 'function') { // << adds `typeof` test so we can pass something other than a function (like a Joi schema) and not have it blow up here but it is usable within the dataType mixin
                err = def.test.call(this, newVal, newType);
                if (err) {
                    throw new TypeError('Property \'' + attr + '\' failed validation with error: ' + err);
                }
            }

I ran a quick spike and this worked perfectly. I was able to create and use a Joi dataType mixin. The benefit of this change is that it allow much more powerful dataType mixing even outside of Joi. Further, the above changes does not break anything and the tests continue to pass.

If this looks good, I’ll make it a PR.

1reaction
dazldcommented, Sep 26, 2014

This would be a wonderful thing as a mixin, great idea.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - Feature idea: Using Joi for prop definition and ...
Feature idea : Using Joi for prop definition and validation, or adding Joi-like features.
Read more >
Chapter 6. Validation with Joi - hapi.js in Action
Working with Joi; Adding validation to hapi.js apps; ... livebook features: ... To run the examples in this chapter, install Joi like any...
Read more >
Joi — awesome code validation for Node.js and Express
This describes how you can use Joi to validate your data. ... Then we added some properties to our object like so: Joi.object().keys({...
Read more >
How To Use Joi for Node API Schema Validation - DigitalOcean
For this tutorial, you will pretend that you are building a school portal and you want to create API endpoints: /people : add...
Read more >
Introduction to joi schema description language - NearForm
js (an application/services framework, much like Express but with more features built in), its sole purpose is the validation of JavaScript ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found