-
-
Notifications
You must be signed in to change notification settings - Fork 37
Description
Hi Team,
For achieving CSP-safe I am creating a standalone validator without depending on AJV on runtime when we use this standalone validator.
Steps I followed-
1)
`const schema = {
description: 'External link',
type: 'object',
additionalProperties: false,
properties: {
name: { type: 'string', maxLength: 25 },
externalLinkType: {
type: 'string',
default: 'Regular',
anyOf: [
{ const: 'Regular', description: 'Link to an External Website' },
{ const: 'DigitalTwin', description: 'Link to a Digital Twin' },
],
},
url: { type: 'string', description: 'Underlying URL', format: 'uri' },
path: { type: 'string', maxLength: 50 },
sampleDate: {
"description": "Datetime model last trained",
"type": [
"null",
"string"
],
"format": "date-time"
}
},
allOf: [
{ required: ['name'] },
{
if: { properties: { externalLinkType: { const: 'Regular' } } },
then: { required: ['url'], not: { required: ['path'] } },
else: { required: ['path'] },
},
],
$id: 'ExternalLink',
};
const ajv = new Ajv({
strict: false,
allErrors: true,
verbose: true,
code: { source: true },
unicode: false,
});
addFormats(ajv);
const validate = ajv.compile(schema);
const moduleCode = standaloneCode(ajv, validate);
const outputDir = join(__dirname, 'src/static/schemas/validators');
const outputPath = join(outputDir, 'validator.js');
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
fs.writeFileSync(outputPath, moduleCode);`
- if I saved file .js and ran . It generated the standalone validator (validator.js) with the method validate.
- Now if I test this in some other script file for some sample data as mentioned below
`
const validate = require('../../src/static/schemas/validators/validator.js');
if (!validate(data1)) console.log('ERRORS 1:', validate.errors);
`
It's working as expected. If I miss some data provided to validate(data1), it's giving error list also.
Note: Now the issue is as mentioned below.
Imagine I want to use validate(data1 within some .ts file of the React Application. I did some changes to the generator script as mentioned within ajv documentation.
`
const ajv = new Ajv({
strict: false,
allErrors: true,
verbose: true,
code: { source: true, esm: true }, // to tell the script for generating the standalone validator file as ESM based.
unicode: false,
});
const outputDir = join(__dirname, 'src/static/schemas/validators');
const outputPath = join(outputDir, 'validator.ts'); // to save the file as .ts
`
Now if I try to import above generated standalone validator within our .ts (React application)
import validate from '../../src/static/schemas/validators/validator.ts';
Expectation is with the above change everything should be fine but
While loading the application it's giving error as
Uncaught TypeError: Cannot read properties of undefined (reading 'fullFormats')
The above error is from the validator.ts file (standalone).
Now my doubts are -
- I can see
const formats4 = require("ajv-formats/dist/formats").fullFormats["date-time"] within validator.ts file. But as informed ajv configuration to generate standalone file as ESM based, then why it's including require("ajv-formats/dist/formats").fullFormats["date-time"] (as it's not related with ESM). - Why it's failing within react application. What's the solution.
Please suggest me a best way to handle this issue which is based on ESM based approach regarding ajv.