-
Notifications
You must be signed in to change notification settings - Fork 184
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
feat: compile ajv validators at build instead of runtime #134
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@@ -0,0 +1,20 @@ | |||
/* eslint-disable */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid a new pattern, can this be moved to a top-level file called ajv.config.js
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so that eslint won't check it? idk i think that would be a little bit of misnaming because it's not a config file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not for eslint, just to avoid more complexity in the directory tree by adding a new scripts
folder. What about just ajv.js
?
const addFormats = require('ajv-formats') | ||
const schema = require('@uniswap/token-lists/dist/tokenlist.schema.json') | ||
|
||
const ajv = new Ajv({ code: { source: true, esm: true } }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not write both schemas to one file (see docs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i was fighting with it because the schemas have the same name, so it didn't like using the same instance. imo not worth the additional time to make it work
/** | ||
* Validates an array of tokens. | ||
* @param json the TokenInfo[] to validate | ||
*/ | ||
export async function validateTokens(json: TokenInfo[]): Promise<TokenInfo[]> { | ||
const validate = (await validator).getSchema(ValidationSchema.TOKENS) | ||
const validate = await loadValidator(ValidationSchema.TOKENS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you consider a single validate(schema: ValidationSchema, data: unknown)
function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i changed it, but i can't say i think it's much better - you lose type specificity and it doesn't save much in terms of LOC
|
||
# generated ajv schema | ||
/src/__generated__/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless you feel strongly about it, I'd rather this be put in something like /src/ajv
or /src/schema
or /src/validators
, that better describes what it is. This is what is done with /src/types/v3
and /src/abis/types
(at the top of the file). I also don't want a separate pattern here for file generation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i do feel like it is useful to know which code is generated vs authored. maybe a "lib" folder is better for that? the more practical reason is for lint/type ignores imo, but also i think you don't necessarily want to read generated code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'd be willing to move all of that code to a generated file path or lib if you agree
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you be willing to put this into /src/validators
, and then follow-up by moving all generated code to a __generated
directory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be cleaned up by making a helper function instead of ajv
and ajv2
, and changing the file structure, but this is definitely good to merge and be cleaned up later.
This reverts commit 4625893.
fixes #51