-
-
Notifications
You must be signed in to change notification settings - Fork 883
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
Standalone code generation generates duplicates of validate functions with referenced functions #1361
Comments
good catch. I think I know why it might be happening - and the normal tests wouldn’t have caught it because JS allows redefining functions, probably even in strict mode... |
@ggoodman it would be great if you could test with the full code you have before I release. I have a suspicion there may be some edge cases when this is not sufficient, but it should be fixed for the majority of cases. |
@epoberezkin I've given it a try and in my limited testing it works as advertised! 🎉 Great work! I love the first-class codegen in v7 ❤️ |
Great - thank you :) |
@epoberezkin this still happens in 7.0.3 if the user schema references back to the info schema like so: import Ajv from 'ajv';
import standaloneCode from 'ajv/dist/standalone';
const userSchema = {
$id: 'user.json',
type: 'object',
properties: {
name: { type: 'string' },
infos: {
type: 'array',
items: { $ref: 'info.json' },
},
},
required: ['name'],
};
const infoSchema = {
$id: 'info.json',
type: 'object',
properties: {
author: { $ref: 'user.json' },
contributors: {
type: 'array',
items: { $ref: 'user.json' },
},
},
required: ['author', 'contributors'],
};
const ajv = new Ajv({
allErrors: true,
code: { optimize: false, source: true, lines: true },
inlineRefs: false, // it is needed to show the issue, schemas with refs won't be inlined anyway
schemas: [userSchema, infoSchema],
});
const moduleCode = standaloneCode(ajv);
console.log(moduleCode); is there any way around this or is this something the library just can't do? |
Need to have a look - mutual recursion is always tricky to compile - but should be possible to fix. I’ll try to reproduce. Just to confirm - it works, but duplicates functions in the module, correct? |
Yes that’s correct. It outputs the correct validators for user and info it just does it twice. |
Can also validate that it happens in
Code does indeed seem the same, just duplicated 😺 |
…code for mutually recursive schemas, #1361
I can reproduce ^^... |
@jafaircl please check the fix with your code too |
@epoberezkin yes the tests pass with my schema as well and copy/pasting the generated validators from this branch into my project doesn't throw any duplicate function errors. Thank you so much! |
…code with mutually recursive schemas (ajv-validator#1418) * test: skipped failing test showing duplicate functions in standalone code for mutually recursive schemas, ajv-validator#1361 * fix: duplicate functions in standalone code for mutually recursive schemas, fixes ajv-validator#1361
What version of Ajv are you using? Does the issue happen if you use the latest version?
7.0.1
Ajv options object
JSON Schema
file:///User.json:
file:///B.json:
Sample data
Not a data validation issue.
Your code
Note that
function validate21
is included twice in the resulting generated code. This breaks some tools while others are unable to remove the duplicate function.Resulting code (prettified)
Validation result, data AFTER validation, error messages
N/A
What results did you expect?
I expected each validation function to be included exactly once.
Are you going to resolve the issue?
I'm trying to wrap my head around the codegen but am unlikely to be able to handle this.
The text was updated successfully, but these errors were encountered: