https://github.com/epoberezkin/ajv/issues/675#issuecomment-357741260
This project was bootstrapped with Create React App.
You might find yourself in a pickle when attempting to provide asynchronous json schema validation via the delightfully performant AJV for a Create React App (CRA) app running in an Internet Explorer (IE) setting.
IE 11 does not support async functions but AJV provides optional transpilation of its generated validation code through ajv-async, which uses NoDent.
After installing ajv-async and running your CRA app you will probably come across this error:
Failed to compile.
./node_modules/nodent/nodent.js
Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
| #!/usr/bin/env node
|
| 'use strict';
You can reproduce this error for the app in this repository by importing
./validatorWithAjvAsync
as thevalidator
withinApp.js
.
Ok, no problem, you can just do what it says and write a Webpack loader of your own. Oh wait, doing this the right way (no hacky code) would involve ejecting from CRA and you may not want to do that NoDent is a node program and can’t run in the browser.
At this point, you have 3 options:
You can test this by removing
$async: true
fromschema.json
.
processCode
option (see AJV Options).Transpilation can be handled by babel-standalone. You can test it by importing
./validatorWithBabel
as thevalidator
withinApp.js
. This solution only serves as a demonstration and does not include an environment check; it will run as-is on all browsers.
./validateCompiled.js
was generated by runningnpm run ajv-compile
. Generating validation functions is still a BETA feature of ajv-cli; changes have been made to the generated code. These changes include disabling eslint and using es6 exports. You can import it withinApp.js
to test solution 5.