Skip to content

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

Closed

Description

@ejmudrak

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

Activity

  1. ejmudrak commented on Jun 21, 2022

    @ejmudrak
    Author

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

    albums: {
      type: 'array',
      items: { 
          type: 'object',
          properties: albumResultSchema.definition.properties,
      },
    },
    
  2. changed the title [-]Feathers schema associations using $ref may not be working[/-] [+][@feathersjs/schema] Feathers schema associations using $ref may not be working[/+] on Jun 30, 2022
  3. AshotN commented on Sep 29, 2022

    @AshotN
    Contributor

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

  4. daffl commented on Oct 5, 2022

    @daffl
    Member

    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 ]
    }>
  5. RickEyre commented on Nov 1, 2023

    @RickEyre

    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.

  6. opatajoshua commented on Feb 20, 2024

    @opatajoshua

    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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions