Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option compile from addKeyword doesn't change on a remove and then add again #2411

Open
rgpro007 opened this issue Apr 8, 2024 · 0 comments

Comments

@rgpro007
Copy link

rgpro007 commented Apr 8, 2024

Here my issue (if it's one):

I setup a keyword with addKeyword and the compile option. I have some external variables used inside. At the first iteration everything is ok, but at the second one we see the variables doesn't change. Is it a normal behavior or an issue ?

What version of Ajv are you using?

version 8.12.0

Your code

const AjvModule = require('ajv/dist/2020');
const express = require('express');

const ajv = new AjvModule();
const schemaName = 'schemaTest';
const keyword = "variableKeywork";

ajv.addSchema({
    "description": "test",
    "type": "object",
    "properties": {
        "item1": {
            "type": "string"
        },
        "item2": {
            "type": "integer"
        }
    },
    "variableKeywork": true
}, schemaName);


const app = express();
const desc = {
    item1: "test",
    item2: 1
}

app.get(`/test`, (req, res) => {
    desc.item1 = req.query.param;
    const time = new Date();
    console.log(`Date outside addKeyword: ${time}`);
    console.log(`sentTo2 outside addKeyword : ${req.query.sentTo2}`);
    console.log(`keyword present : ${JSON.stringify(ajv.getKeyword(keyword))}`);
    ajv.addKeyword({
        keyword: keyword,
        modifying: true,
        compile: (value) => function validate(data, root) {
            console.log(`Date inside addKeyword: ${time}`);
            console.log(`sentTo2 inside addKeyword : ${req.query.sentTo2}`);
            if (Boolean(req.query.sentTo2)) {
                root.rootData.item2 = req.query.param;
            }
            console.log(JSON.stringify(root.rootData))
            return true
        }
    });
    console.log(`keyword present : ${JSON.stringify(ajv.getKeyword(keyword))}`);
    const isDataValid = ajv.validate(schemaName, desc);
    ajv.removeKeyword(keyword);
    console.log(`keyword present : ${JSON.stringify(ajv.getKeyword(keyword))}`);
    res.json(isDataValid);
});

app.listen(8080, function () {
    console.log("serveur started");
});

// try with http://localhost:8080/test?param=1&sentTo2=true
// and then  http://localhost:8080/test?param=1&sentTo2=false

Output obtained

serveur started
Date outside addKeyword: Mon Apr 08 2024 17:24:53 GMT+0200 (Central European Summer Time)
sentTo2 outside addKeyword : true
keyword present : false
keyword present : {"keyword":"variableKeywork","modifying":true,"type":[],"schemaType":[]}
Date inside addKeyword: Mon Apr 08 2024 17:24:53 GMT+0200 (Central European Summer Time)
sentTo2 inside addKeyword : true
{"item1":"1","item2":"1"}
keyword present : false


Date outside addKeyword: Mon Apr 08 2024 17:25:18 GMT+0200 (Central European Summer Time)
sentTo2 outside addKeyword : false
keyword present : false
keyword present : {"keyword":"variableKeywork","modifying":true,"type":[],"schemaType":[]}
Date inside addKeyword: Mon Apr 08 2024 17:24:53 GMT+0200 (Central European Summer Time)
sentTo2 inside addKeyword : true
{"item1":"1","item2":"1"}
keyword present : false

What results did you expect?

the ajv.removeKeyword really remove the keyword and allow us the define it again.

Are you going to resolve the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant