ajv-validator/ajv

RE2 integration incorrectly typed

Closed this issue · 2 comments

What version of Ajv are you using? Does the issue happen if you use the latest version?

Yes - issue happens with the latest version (currently v8.17.1).

Your typescript code

import RE2 from "re2";
import Ajv from "ajv";

const ajv = new Ajv({
  code: { regExp: RE2 },
});

Typescript compiler error messages

Property 'code' is missing in type 'RE2Constructor' but required in type 'RegExpEngine'.  ts(2741)

Describe the change that should be made to address the issue?

The code property should potentially be marked as optional in the RegExpEngine interface since it is "only needed for standalone code generation":

code: string

Are you going to resolve the issue?

Happy to submit a PR if that is desired.

Hi @ramijarrar it does indeed look like a typescript issue as the js version of this same code runs fine. I'm happy for you to submit and PR and I'll try and get @epoberezkin opinion on it.

After discussing with the author @ramijarrar I have discovered that it is preferred to keep this property as required, but you can work around it either like this:

import RE2 from "re2";
import Ajv from "ajv";

const re2other = (pattern: string, u: string) => RE2(pattern, u);
re2other.code = 'require("ajv/dist/runtime/re2").default';

const ajv = new Ajv({
  code: { regExp: re2other },
});

Or use the same approach the tests use to create a separate module to wrap RE2 and then require it where needed.