Anil Bairi
Anil Bairi

Reputation: 1

AJV user defined keyword using "code" function

Anyone know how can i use AJV user defined keyword feature using "code" function? Currently i am using "validate" function which works fine but which needs to replaced with "code" function as I am moving the schema compilation to build time due 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" function and I have few custom keywords in my application. I am bit struggling in replacing the "validate" function as not sure how to read the data and do the comparision like below. Examples in the documentation are very basic minimal...

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: screenshot in debugger

Thanks in advance! Anil

I need to replcae the validate function with code function in custom keyword and still do the dates comparison

Upvotes: 0

Views: 27

Answers (0)

Related Questions