Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjectSchema.extend Does not return a schema instance #249

Open
2 tasks done
esatterwhite opened this issue Jul 8, 2024 · 3 comments
Open
2 tasks done

ObjectSchema.extend Does not return a schema instance #249

esatterwhite opened this issue Jul 8, 2024 · 3 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@esatterwhite
Copy link
Contributor

esatterwhite commented Jul 8, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.x.x

Plugin version

4.2.1

Node.js version

18.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Manjaro 5.10.218 x86

Description

Once you call extend on a schema, the function chain is broken because the extend function returns a plain object rather than an instance of the schema being extended. This breaks the normal usage of the builder pattern implemented by this library.

Link to code that reproduces the bug

return { valueOf, isFluentSchema, FLUENT_SCHEMA, _getState, extend }

Expected Behavior

One should be able to continue chaining function calls such as without or only, etc after calling extend.

It should be safe to use the schema factory function found on the schema to build out a new instance of the same type.

@mcollina
Copy link
Member

mcollina commented Jul 9, 2024

Thanks for reporting!

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

@esatterwhite
Copy link
Contributor Author

var fluent = require('fluent-json-schema')
var base = fluent
  .object()
  .prop('one', fluent.number())
  .prop('two', fluent.number())

var extended = fluent.object()
  .prop('three', fluent.number())
  .prop('four', fluent.number())
  .extend(base)

var alternate = extended.without(['one'])
var alternate = extended.without(['one'])
                         ^

TypeError: extended.without is not a function
    at Object.<anonymous> (/home/esatterwhite/dev/js/logdna/auth-provider-service/error.js:12:26)
    at Module._compile (node:internal/modules/cjs/loader:1369:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
    at Module.load (node:internal/modules/cjs/loader:1206:32)
    at Module._load (node:internal/modules/cjs/loader:1022:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49

Node.js v20.12.2

@mcollina mcollina added bug Something isn't working good first issue Good for newcomers labels Jul 9, 2024
@mcollina
Copy link
Member

mcollina commented Jul 9, 2024

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants