feathers icon indicating copy to clipboard operation
feathers copied to clipboard

Fix for Ajv global collision bug #2681

Open FossPrime opened this issue 3 years ago • 2 comments

Summary

Fixes HMR issues with Ajv's schemas #2681

Sandbox with fix applied: https://stackblitz.com/edit/feathers-vite-chat-pcztgp?file=src%2Fschema%2Fusers.schema.ts,schema%2Fsrc%2Fschema.ts

Repo with fix: https://github.com/MarsOrBust24/feathers-schema-freedom Chat: https://discord.com/channels/509848480760725514/989366511665819698/999577745010999426

Alternatively we could instantiate Ajv inside the constructor, but it would have a performance penalty.

P.S. Type coercion by default seems like a bad idea, it will hide problems until it's too late and make debugging more difficult.

FossPrime avatar Jul 21 '22 07:07 FossPrime

As to how to better support refs, we could add an addSchema method to the returned class. Which would then allow users to combine schemas with an import tree. This is much closer to the syntax Ajv uses.

// users.schema.ts
export const schema = new FeathersSchema()
export const UserSchema = schema.addSchema(...)
const UserResponseSchema = UserSchema.fo(...)
---
// messages.schema.ts
import { schema } from './users.schema.ts'
export const MessageSchema = schema.addSchema(...)

In that example UserSchema shares the same Ajv with MessageSchema. FeathersSchema would be a small class that stores an Ajv instance and takes Ajv parameters in it's constructor.

This could be implemented side by side with the current API, with a deprecation warning for anyone using the old unsafe API.

FossPrime avatar Jul 22 '22 19:07 FossPrime

I agree on the type coercion - it's mainly intended for query schemas since they generally come in as strings for REST calls. Not sure what the best setup would be. addSchema sounds interesting. How does this usually work in AJV?

daffl avatar Jul 25 '22 20:07 daffl