feathers icon indicating copy to clipboard operation
feathers copied to clipboard

[@feathersjs/schema] Feathers schema associations using $ref may not be working

Open ejmudrak opened this issue 3 years ago • 1 comments

Steps to reproduce

When using $ref associations with @feathersjs/schema and compiling the app locally, it fails and gives an error about the association.

The error:

Error: can't resolve reference AlbumResult from id ArtistResult
    at Object.code (/app/node_modules/ajv/lib/vocabularies/core/ref.ts:19:39)
    at keywordCode (/app/node_modules/ajv/lib/compile/validate/index.ts:523:9)
    at /app/node_modules/ajv/lib/compile/validate/index.ts:228:21
    at CodeGen.code (/app/node_modules/ajv/lib/compile/codegen/index.ts:525:33)
    at CodeGen.block (/app/node_modules/ajv/lib/compile/codegen/index.ts:680:20)
    at schemaKeywords (/app/node_modules/ajv/lib/compile/validate/index.ts:228:9)
    at typeAndKeywords (/app/node_modules/ajv/lib/compile/validate/index.ts:161:3)
    at subSchemaObjCode (/app/node_modules/ajv/lib/compile/validate/index.ts:147:3)
    at subschemaCode (/app/node_modules/ajv/lib/compile/validate/index.ts:124:7)
    at KeywordCxt.subschema (/app/node_modules/ajv/lib/compile/validate/index.ts:491:5)

Here is a gist of my app's schema and hooks code, which is currently reproducing the error.

System configuration

Running Feathers in docker

Module versions (especially the part that's not working): I may be in a unique position since I am running Feathers v4 for all but the@feathersjs/schema package, which is v5

    "@feathersjs/authentication": "^4.5.11",
    "@feathersjs/authentication-local": "^4.5.15",
    "@feathersjs/authentication-oauth": "^4.5.15",
    "@feathersjs/configuration": "^4.5.15",
    "@feathersjs/errors": "^4.5.11",
    "@feathersjs/express": "^4.5.11",
    "@feathersjs/feathers": "^4.5.11",
    "@feathersjs/schema": "^5.0.0-pre.23",
    "@feathersjs/socketio": "^4.5.15",
    "@feathersjs/transport-commons": "^4.5.11",

NodeJS version: 17.0.0 Operating System: macOS Monterey 12.2.1

ejmudrak avatar Jun 21 '22 17:06 ejmudrak

My temporary workaround is importing the referenced schema directly like so:

albums: {
  type: 'array',
  items: { 
      type: 'object',
      properties: albumResultSchema.definition.properties,
  },
},

ejmudrak avatar Jun 21 '22 17:06 ejmudrak

Were you able to resolve this issue? I am running into something similar and I'm unsure if it's user error.

AshotN avatar Sep 29 '22 17:09 AshotN

This has been addressed now in #2772 with making the schema wrapper optional and using FromSchema or TypeBox directly which both do support references. With plain JSON schema this would look like this:

export const userSchema = {
  $id: 'User',
  type: 'object',
 // etc.
} as const

// ...

import { FromSchema } from '@feathersjs/schema'

export const messageSchema = {
  $id: 'Message',
  type: 'object',
  additionalProperties: false,
  required: ['text'],
  properties: {
    text: {
      type: 'string'
    },
   user: {
    $ref: 'User'
   }
 }
} as const

export type Message = FromSchema<typeof messageSchema, {
  references: [ typeof userSchema ]
}>

daffl avatar Oct 05 '22 21:10 daffl

Is there a more complete example for this solution? I'm having trouble understanding how you go from this example to actually using getValidator and getDataValidator and their respective hooks.

RickEyre avatar Nov 01 '23 17:11 RickEyre

Error is thrown by getValidator

  1. Option 1: Omit refs from your query shema if you don't need them in query validation
export const postQueryProperties = Type.Partial(Type.Omit(postSchema, ["user"])); // where user is a ref for userSchema
  1. Option2: addSchema before getValidator is called
queryValidator.addSchema(userSchema);
export const postQueryValidator = getValidator(postQuerySchema, queryValidator);

opatajoshua avatar Feb 20 '24 22:02 opatajoshua