feathersjs / feathers

The API and real-time application framework
https://feathersjs.com
MIT License
15.03k stars 750 forks source link

[cli] register main schemas using dataValidator.addSchema(<service>Schema) #2993

Closed miguelrk closed 1 year ago

miguelrk commented 1 year ago

TLDR: CLI should be udpated to register main schemas using dataValidator.addSchema(<service>Schema) to be able to use Type.Ref(<service>Schema)

Relevant thread

Steps to reproduce

I'm getting the same Error MissingRefError: can't resolve reference User from id Message when using user: Type.Ref(userSchema) for the following:

const userSchema = Type.Object({
    // ...
  }, { $id: 'User', additionalProperties: false }
)
const messageSchema = Type.Object({
    userId: Type.String(),
    user: Type.Ref(userSchema)
  }, { $id: 'Message', additionalProperties: false }
)

Expected behavior

Main schemas (and all others: data, patch and query) should be registered using ajv's addSchema()

Actual behavior

I have tried the following for user instead of Type.Ref(userSchema) to no success:

// throws "Error: reference "User" resolves to more than one schema
user: userSchema
// throws "MissingRefError"
user: Type.Ref(userSchema, { $id: 'User' })
// does not work
user: Type.Ref(userSchema, { $schema: 'User' })

System configuration

"@feathersjs/adapter-commons": "^5.0.0-pre.35",
"@feathersjs/authentication": "^5.0.0-pre.35",
"@feathersjs/authentication-client": "^5.0.0-pre.35",
"@feathersjs/authentication-oauth": "^5.0.0-pre.35",
"@feathersjs/configuration": "^5.0.0-pre.35",
"@feathersjs/errors": "^5.0.0-pre.35",
"@feathersjs/express": "^5.0.0-pre.35",
"@feathersjs/feathers": "^5.0.0-pre.35",
"@feathersjs/mongodb": "^5.0.0-pre.35",
"@feathersjs/schema": "^5.0.0-pre.35",
"@feathersjs/socketio": "^5.0.0-pre.35",
"@feathersjs/transport-commons": "^5.0.0-pre.35",
"@feathersjs/typebox": "^5.0.0-pre.35",
miguelrk commented 1 year ago

For reference, adding dataValidator.addSchema(<service>Schema) right after declaring <service>Schema gets it working. For example:

// user.schema.ts
const userSchema = Type.Object({
    // ...
  }, { $id: 'User', additionalProperties: false }
)
dataValidator.addSchema(userSchema) // ADD THIS LINE HERE

// message.schema.ts
import { userSchema } from "../users/user.schema"

const messageSchema = Type.Object({
    userId: Type.String(),
    user: Type.Ref(userSchema)
  }, { $id: 'Message', additionalProperties: false }
)