Skip to content

ability to generate standalone code from the callback specified in the validate property #2517

Open
@denisovan31415

Description

If a callback function inputted to the validate property is self contained, then it could be used to generate the standalone code.

Why not allow the client to specify a flag that would use the validate callback to generate standalone code?

ajv.addKeyword({
  keyword: "even",
  type: "number",
  schemaType: "boolean",
  // $data: true // to support [$data reference](./guide/combining-schemas.md#data-reference), ...
  validate: (schema, data) => {
    return true;
  },
  code(cxt) {
    const { data, schema } = cxt;
    const op = schema ? Ajv._`!==` : Ajv._`===`;
    cxt.fail(Ajv._`${data} %2 ${op} 0`); // ... the only code change needed is to use `cxt.fail$data` here
  },
});

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions