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

Closed
rgpro007 opened this issue Apr 8, 2024 · 6 comments
Closed
Labels

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

@jasoniangreen
Copy link
Collaborator

I got to the bottom of the issue @rgpro007. Here you can see my minimal example that reproduces the issue and then fixes it. The reason you are hitting this issue is that for performance reasons, AJV caches a lot of things. In this case you have a cached schema which has already been compiled. When you try to compile the same schema again, it skips certain steps to save time.

The simple solution is to use ajv.removeSchema (see line 21) before you try and compile it again. Alternatively you could clone the schema {...schema} which would also work because the cache uses a WeakMap which recognises the schema by reference.

@jasoniangreen
Copy link
Collaborator

After reviewing the docs I am confident this is how it is intended to be and the docs are clear enough on how you should only compile schemas once. So I will close this one and leave you with two links:

@rgpro007
Copy link
Author

Thank for you help.
I will modify my code.

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

No branches or pull requests

2 participants