Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] Add parameters pattern support #13

Closed
DenisFerrero opened this issue Nov 22, 2021 · 1 comment
Closed

[FR] Add parameters pattern support #13

DenisFerrero opened this issue Nov 22, 2021 · 1 comment

Comments

@DenisFerrero
Copy link

Feature request - Parameters pattern

Hi, I need to have a support for indicating pattern like validation in the parameters. For example:

password: { type: 'string', min: 8, pattern: /^[a-zA-Z0-9]+$/ }

or

password: { type: 'string', min: 8, pattern: '^[a-zA-Z0-9]+$' }

The patterns can be declared as RegExp (first example) or as a String (second example).

I tried to do something by myself and I realized this solution:

if (node.pattern && (node.pattern.length > 0 || node.pattern.source.length > 0))
  out.pattern = new RegExp(node.pattern).source;

I applied this code in the getTypeAndExample method, just before return out:

getTypeAndExample(node) {
  if (!node) {
    node = {};
  }
  let out = {};

  ...

  out.minLength = node.length || node.min;
  out.maxLength = node.length || node.max;

  /**** New code - Add pattern support ****/
  if (node.pattern && (node.pattern.length > 0 || node.pattern.source.length > 0))
    out.pattern = new RegExp(node.pattern).source;

  return out;
},

Can this solution be valid? Or it's necessary to apply something else in other places? If the solution is correct can you add this feature to your code?

grinat added a commit that referenced this issue Jan 10, 2022
@grinat
Copy link
Owner

grinat commented Jan 10, 2022

@DenisFerrero released in v1.1.0

@grinat grinat closed this as completed Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants