User defined formats with standalone validation code
See original GitHub issueThe 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:
- Created 3 years ago
- Comments:17 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Slapbox RegExp format support in standalone code added in v8.0.2
Still worth amending the docs, for the formats defined as functions.
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,