ajv-validator / ajv-validator/ajv-cli

CLI compile - problem with CommonJS imports/exports and nonexistent require() paths

Open
#98 4 comments 0 reactions 0 assignees View on GitHub
compatibility
Dominant language
TypeScript
Stars
316
Forks
77
PR merge metrics
No merged PRs in 30d

Description

Hello!

In our project, we're using a typescript monorepo setup with a shared model.
The model interfaces are annotated with [YousefED/typescript-json-schema](https://github.com/YousefED/typescript-json-schema) to produce json schemas in build time. These schemas are later used on the API level, as well as complementary validation of the forms in an Angular frontend, using AJV. So far, so good.

We ran into a problem with Content Security Policy and unsafe-eval.
As a remedy, I'm trying to use AJV CLI to precompile validator functions. That, however, leads to a couple issues. It is generating some code, but there are caveats.

1. Generated validator files use CommonJS syntax for both imports and exports. Have there been any plans to support ES style imports/exports in these?
2. Looks like some of the generated require() style imports are trying to resolve nonexistent paths, resulting in errors. For example, validator generated from a schema with minlength:
`var func8 = require("ajv/dist/compile/ucs2length").default;`
In reality, there's no such file under ajv/dist/, but there's one under ajv/lib/
3. Other than that, it's misleading that "precompiled standalone validator functions" have an implicit dependency on AJV itself. Shouldn't it be possible to compile with a flag that includes the dependencies directly in the generated file? Or at least allow it to use ES-style imports rather than CommonJS?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.