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

AJV user defined keyword using "code" function #2525

Open
AnilBairi opened this issue Jan 8, 2025 · 0 comments
Open

AJV user defined keyword using "code" function #2525

AnilBairi opened this issue Jan 8, 2025 · 0 comments

Comments

@AnilBairi
Copy link

What version of Ajv are you using? Does the issue happen if you use the latest version?
v8.17.1

Currently, I am using "validate" function in a user defined keyword, which works fine but which needs to be replaced with "code" function as I am moving the schema compilation to build time due to CSP Exceptions in runtime like mentioned here https://ajv.js.org/standalone.html. AJV standalone validation code with user defined keywords supports only "code" and "macros" functions, and I have few custom keywords in my application. I am a bit struggling in replacing the "validate" function as not sure how to read the data and do the comparison like below.

Schema:

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "src/schemas/test.schema.json",
    "type": "object",
    "properties": {
        "validFrom": {
            "type": "string",
            "format": "ISO-8601",
            "compareDates": {
                "$data": "1/validTo"
            },
            "examples": ["2022-10-11T10:27:14Z", "2022-10-11T10:27Z"]
        },
        "validTo": {
            "type": "string",
            "format": "ISO-8601",
            "examples": ["2022-10-11T10:27:14Z", "2022-10-11T10:27Z"]
        }
    }
}

Current: working keyword "compareDates"

const ajv = new Ajv({
                allErrors: true,
                $data: true,
                schemas: schemas
            });
...
ajv.addKeyword({
        keyword: 'compareDates',
        type: 'string',
        $data: true,
        validate: (targetDate: Date, sourceDate: Date) => {
            const timeForTargetDate = new Date(targetDate).getTime();
            const timeForSourceDate = new Date(sourceDate).getTime();
            if (timeForSourceDate >= timeForTargetDate) {
                return false;
            }
            return true;
        },
        error: {
            message: 'invalid dates'
        }
    })

Required:

const ajv = new Ajv({
                allErrors: true,
                $data: true,
                schemas: schemas,
                code: {
                    source: true,
                    formats: require('./formats')
                }
            });
...
ajv.addKeyword({
        keyword: 'compareDates',
        type: 'boolean',
        schemaType: 'boolean',
        $data: true,
        code(cxt) {
            const { data, $data, schema } = cxt;
            //$data does not have validTo property value(see screenshot)
            const validFrom = data;
            const validTo = $data;
            gen.code(_`console.log(${validFrom})`); // prints '2022-01-01T00:00:01.000Z'
            gen.code(_`console.log(${validTo})`); // prints "1/validTo" ???
        },
        errors: false
    });

context screenshot:
image

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