question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

User defined formats with standalone validation code

See original GitHub issue

The error we’re seeing is:

Error: CodeGen: "code" for formats0 not defined

Oddly this error does not occur if we use Ajv in our app, it only occurs if we try to make a standalone validator script.

What version of Ajv are you using? Does the issue happen if you use the latest version? 7.1.1 (latest)

Ajv options object

const ajv = new Ajv({
  code: {
    source: true, // this option is required to generate standalone code
  },
  removeAdditional: true,
});

JSON Schema

const demoSchema = {
  $id: 'demoSchema',
  additionalProperties: false,
  properties: {
    test: hexCodeValidator,
  },
};

Sample data

N/A - bug occurs during compilation of standalone Ajv.

Your code

Repro

Are you going to resolve the issue?

I cannot resolve the issue.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
epoberezkincommented, Mar 31, 2021

@Slapbox RegExp format support in standalone code added in v8.0.2

Still worth amending the docs, for the formats defined as functions.

2reactions
epoberezkincommented, Feb 28, 2021

Custom format functions support with standalone code is limited.

If simply using regular expression (not a function) works for your case - it works for the example, but maybe you have a more complex function, - then passing regular expression directly to addFormat would make it work.

If you do need a function then there is an instance option (see Options in api.md) to which you need to pass a “code snippet” that would result into an object with all formats - see ajv-formats here for the example how it can be done:

https://github.com/ajv-validator/ajv-formats/blob/master/src/index.ts#L55

An obvious improvement is to allow defining custom formats individually and pass “code snippets” for standalone code in their definitions.

It’s not well documented,

Read more comments on GitHub >

github_iconTop Results From Across the Web

Format validation - Ajv JSON schema validator
Formats and standalone validation code​​ If you define your own formats, for standalone code generation to work you need to pass the code...
Read more >
Example of Setting Up User-Defined Validations for Payment ...
This example demonstrates how to set up two user-defined validations. You must set up two validations when you want your user-defined validations to:....
Read more >
TypeScript Type Safety with AJV Standalone
The type definition files informs TS how to use the JS files in the ... Create the AJV validation code in ESM format...
Read more >
236-29: Building and Using User Defined Formats
Formats are created using PROC FORMAT. User defined formats can be used to: ! convert numeric variables into character values !
Read more >
Ajv: Another JSON schema validator - npm
NEW: support of JSON Type Definition RFC8927 (from v7.1.0), ... asynchronous validation of user-defined formats and keywords ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found