Joi: Described schemas don't have Regex

Created on 11 Feb 2019  路  11Comments  路  Source: sideway/joi

Context

  • node version: 10.14.2
  • joi version: 14.3.1
  • environment (node, browser): node
  • used with (hapi, standalone, ...): standalone
  • any other relevant information: n/a

What are you trying to achieve or the steps to reproduce ?

const Joi = require('joi');

const schema = Joi.object().keys({
  age: Joi
    .string()
    .regex(/^[0-9]{2}$/)
    .required(),
});

const result = Joi.describe(schema);
console.log(result);

Which result you had ?

The regex property is empty 馃槩

What did you expect ?

The regex property to be populated with something along the lines of /^[0-9]{2}$/.

support

All 11 comments

You don't need to be calling .compile on a schema like that. Compile should really only be used for trying to make a best guess at a schema from some type of input.

What are you trying to do here? Maybe we can point you in the right direction.

In the meantime, if you are just dead-set on retrieving the regex from that doubly-compiled schema, you can leverage the .describe() method:

>Joi.compile(schema).describe().children.age.rules;
//[ { name: 'regex', arg: { pattern: /^[0-9]{2}$/ } } ]

@WesTyler Apologies, I had created this issue in a rush. I've updated to include what I'm actually doing which is indeed using .describe()

Ah, ok, thank you.

Did you find where the regex pattern lives in the description (schema.describe().children.age.rules)? Were you expecting it in the root somewhere, and if so, what lead you to that expectation?

Hi @WesTyler,
Thanks for getting back to me. I've managed to track the cause of this issue, which is the result of sending Joi.describe(schema) as JSON. This is causing the pattern property to be blank.

Please see the following for an example: https://runkit.com/mindvox/5c2e2f7cc1568500129254b7

I'm not sure what the suggested approach for this would be as what I'm trying to achieve are shared schemas.

Ah okay, yeah, JSON.stringify doesn't play nicely with RegExps

> JSON.stringify(/abc/)
//'{}'

what I'm trying to achieve are shared schemas.

Are you trying to share the schemas within a single codebase, across multiple private repos you own, or across services you may not own via http?

馃 yep, they are shared via HTTP

The solution in this instance is to simply use the following:

RegExp.prototype.toJSON = RegExp.prototype.toString;

Feel free to close, I've updated the Runkit example above to include the solution

You are honestly probably up a creek without a paddle, then. :

Joi objects are not designed to play nicely with being transferred over http like this. The describe() output is good, but then you need some sort of reverse-parsing on the receiving end to turn the described schema back into a usable Joi schema.

If you own (or trust) both the requesting and the sending applications, I would suggest creating a shared library (private npm package) that exposes the raw Joi schema that can be versioned and added as a dependency by both applications. This is how we handle shared schema inside of our organization and it's been working great for about 3 years now.

For the purposes of this issue, though, I am going to close this since the main issue appears to have been answered. Feel free to open a "sharing schema across applications" issue if you want to discuss further. :)

Thanks @WesTyler, I agree this maybe more trouble than it's worth. I'll look to create a private NPM package for shared schemas. Thanks for the insight, it's most appreciated

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

Was this page helpful?
0 / 5 - 0 ratings