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 · 3 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');
const ajv = new AjvModule();

const keyword = "variableKeywork";
const schema = {
    "description": "test",
    "type": "object",
    "properties": {
        "item1": {
            "type": "string"
        }
    },
    "variableKeywork": true
};

test({
    item1: "test"
}, true);
console.log('-------------------------');

setTimeout(() => {
    test({
        item1: 1
    }, false);
}, 5000);

function test(desc, value) {
    const now = new Date();
    const time = now.getTime();
    console.log(`Current time: ${time}`);
    ajv.addKeyword({
        keyword: keyword,
        modifying: true,
        compile: (value) => function validate(data, root) {
            console.log(`Time inside addKeyword: ${time} and value = ${value}`);
            return true
        }
    });
    console.log(`keyword present : ${JSON.stringify(ajv.getKeyword(keyword))}`);
    const validate = ajv.compile(schema);
    const isDataValid = validate(desc);
    ajv.removeKeyword(keyword);
    console.log(`data valid: ${isDataValid}`)
    console.log(`keyword present: ${JSON.stringify(ajv.getKeyword(keyword))}`);
}

Output obtained

Current time: 1716967536094
keyword present : {"keyword":"variableKeywork","modifying":true,"type":[],"schemaType":[]}
Time inside addKeyword: 1716967536094 and value = true
data valid: true
keyword present: false
-------------------------
Current time: 1716967541145
keyword present : {"keyword":"variableKeywork","modifying":true,"type":[],"schemaType":[]}
Time inside addKeyword: 1716967536094 and value = true
data valid: false
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?

@jasoniangreen
Copy link
Collaborator

Can you please produce a minimal example using this runkit template? It should be possible to reproduce the issue without express.

@rgpro007
Copy link
Author

Hello,
I have updated the example with a runkit template : runkit example

@jasoniangreen
Copy link
Collaborator

I have looked into this and I have been able to replicate with an even simpler example.
https://runkit.com/jasoniangreen/addkeyword-bug-2411

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

No branches or pull requests

2 participants