Skip to content

Commit

Permalink
fix: crash for regex without unicode in schema (#274)
Browse files Browse the repository at this point in the history
* fix: crash for regex without unicode in schema

* Create nice-paws-rush.md
  • Loading branch information
ota-meshi committed Dec 11, 2023
1 parent 9ada5e2 commit 296b9d1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-paws-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-json-schema-validator": patch
---

fix: crash for regex without unicode in schema
2 changes: 2 additions & 0 deletions src/utils/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
SchemaObject,
ValidateFunction,
} from "ajv";
import type { RegExpEngine } from "ajv/dist/types";
import Ajv, { _, str } from "ajv";

export default Ajv;
Expand All @@ -15,4 +16,5 @@ export type {
ErrorObject,
SchemaObject,
ValidateFunction,
RegExpEngine,
};
20 changes: 20 additions & 0 deletions src/utils/validator-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@ import type {
ErrorObject,
SchemaObject,
ValidateFunction,
RegExpEngine,
} from "./ajv";
import { URL } from "url";
import type { RuleContext } from "../types";
import Ajv from "./ajv";
import { draft7 as migrateToDraft7 } from "json-schema-migrate";
import { loadSchema } from "./schema";

// eslint-disable-next-line func-style -- ignore
const lazyRegExpEngine: RegExpEngine = (str, flags) => {
let error: Error;
try {
return new RegExp(str, flags);
} catch (e) {
error = e as never;
}
if (flags.includes("u")) {
return new RegExp(str, flags.replace("u", ""));
}
throw error;
};
lazyRegExpEngine.code = "new RegExp";

const ajv = new Ajv({
// schemaId: "auto",
allErrors: true,
Expand All @@ -19,6 +35,9 @@ const ajv = new Ajv({
// extendRefs: "ignore",
logger: false,
strict: false,
code: {
regExp: lazyRegExpEngine,
},
});
// ajv.addMetaSchema(require("ajv/lib/refs/json-schema-draft-04.json"))
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports -- ignore
Expand Down Expand Up @@ -85,6 +104,7 @@ function schemaToValidator(
if (resolveError(e, schemaPath, schemaObject, context)) {
continue;
}
console.error(schemaPath);

Check warning on line 107 in src/utils/validator-factory.ts

View workflow job for this annotation

GitHub Actions / Release

Unexpected console statement

Check warning on line 107 in src/utils/validator-factory.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
throw e;
}
break;
Expand Down

0 comments on commit 296b9d1

Please sign in to comment.