feathersjs / feathers

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

npx feathers generate service fails with unknown keyword: "objectid" #3099

Closed tvld closed 1 year ago

tvld commented 1 year ago

Server is humming fine, (latest v5.0.0) but generating any new service fails:

npx feathers generate service
? What is the name of your service? invites
? Which path should the service be registered on? invites
? Does this service require authentication? No
? What database is the service using? MongoDB
? Which schema definition format do you want to use? Schemas allow to type, validate, secure and populate data TypeBox  (recommended)
    Updated src/client.ts
    Wrote file src/services/invites/invites.schema.ts
    Wrote file src/services/invites/invites.ts
    Updated src/services/index.ts
    Wrote file src/services/invites/invites.shared.ts
    Wrote file test/services/invites/invites.test.ts
    Wrote file src/services/invites/invites.class.ts

Gives when npm run dev:

my_server/node_modules/ajv/dist/compile/index.js:120
        throw e;
        ^

Error: strict mode: unknown keyword: "objectid"
    at checkStrictMode (my_server/node_modules/ajv/dist/compile/util.js:174:15)
    at checkUnknownRules (my_server/node_modules/ajv/dist/compile/util.js:32:13)
    at alwaysValidSchema (my_server/node_modules/ajv/dist/compile/util.js:19:5)
    at my_server/node_modules/ajv/dist/vocabularies/code.js:110:75
    at Array.some (<anonymous>)
    at Object.validateUnion [as code] (my_server/node_modules/ajv/dist/vocabularies/code.js:110:32)
    at keywordCode (my_server/node_modules/ajv/dist/compile/validate/index.js:464:13)
    at my_server/node_modules/ajv/dist/compile/validate/index.js:222:17
    at CodeGen.code (my_server/node_modules/ajv/dist/compile/codegen/index.js:439:13)
    at CodeGen.block (my_server/node_modules/ajv/dist/compile/codegen/index.js:568:18)

I do not use the word "objectid" in my code... I will ask around on Discord, but it seems some kind of bug for sure...

tvld commented 1 year ago

When in the schema file, I replace :

   // _id: ObjectIdSchema(), // this causes the error
    _id: Type.Number(), // >> this works

Weird, as all my other services use _id: ObjectIdSchema(), happily

daffl commented 1 year ago

I was going to say that it needs the objectid keyword (https://feathersjs.com/api/databases/mongodb.html#ajv-keyword) in the validators file but it looks like that is already in there?

tvld commented 1 year ago

In validators I have:

const formats: FormatsPluginOptions = [
  'date-time',
  'time',
  'date',
  'email',
  'hostname',
  'ipv4',
  'ipv6',
  'uri',
  'uri-reference',
  'uuid',
  'uri-template',
  'json-pointer',
  'relative-json-pointer',
  'regex'
]

I can not add 'objectid' to that array as TS complains: Type '"objectid"' is not assignable to type 'FormatName' Plus, in that case why would all my other _id: ObjectIdSchema() work nicely?

My whole project gives zero hits on searching for objectid, all lowercase.

marshallswain commented 1 year ago

Do you have this code in your validators file?

import { keywordObjectId } from '@feathersjs/mongodb'

const validator = new Ajv()

validator.addKeyword(keywordObjectId)

That code should prevent the error message you're seeing.

tvld commented 1 year ago
// validator.ts is now:

import { keywordObjectId } from '@feathersjs/mongodb'
// For more information about this file see https://dove.feathersjs.com/guides/cli/validators.html
import { Ajv, addFormats } from '@feathersjs/schema'
import type { FormatsPluginOptions } from '@feathersjs/schema'

const formats: FormatsPluginOptions = [
  'date-time',
  'time',
  'date',
  'email',
  'hostname',
  'ipv4',
  'ipv6',
  'uri',
  'uri-reference',
  'uuid',
  'uri-template',
  'json-pointer',
  'relative-json-pointer',
  'regex'
]

export const dataValidator: Ajv = addFormats(new Ajv({}), formats)

export const queryValidator: Ajv = addFormats(
  new Ajv({
    coerceTypes: true
  }),
  formats
)

dataValidator.addKeyword(keywordObjectId)
queryValidator.addKeyword(keywordObjectId)

I checked in .git and I never touched that file myself

tvld commented 1 year ago

It's very weird. I compared an existing service line-by-line to the new one, and they are practically the same. Starting to get worried something strange is left over from my whole v33-34-35-37 journey...Also considering the 12-second time before the server is running...Could there be multiple ajv's or whatever busy? ... Guessing now )

daffl commented 1 year ago

It does seem like something (possibly simple) is off but it's hard to tell without being able to have a look. If the app isn't that big, a 12 second startup time also seems pretty long.

tvld commented 1 year ago

I would be really happy if you could look at the whole repo, but it's in GitLab. I could add you by email... There are 5 services total now, including the new 'invites'..).

tvld commented 1 year ago

Oops... going to close this... I had two files: ../valdiator.ts and ../schemas/validator.ts And only one got the right keyword registration This is an inheritance from v35 ...