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

Root $id not overrided by S.extend #54

Merged
merged 5 commits into from
Dec 2, 2019
Merged

Conversation

aboutlo
Copy link
Collaborator

@aboutlo aboutlo commented Nov 25, 2019

Unfortunately S.extend doesn't work as expected. I have been too optimistic.

The problem is here: https://github.com/fastify/fluent-schema/blob/master/src/utils.js#L140-L150

Basically, fluent-schema adds attributes to the last prop declared. However with S.extend(base).id('extend') the id will be added to the last prop from the base rather than the root document.

I have the feeling we need to change the syntax to:

S.object()
  .id('extend')
  .prop('bar').extend(S.object()id('base').prop('foo'))

I need to dig into more

For now, this PR contains only the tests to expose the issue

@Eomm
Copy link
Member

Eomm commented Nov 28, 2019

Could you use symbols to ignore prop that has been imported/extented in the main S.object?

@aboutlo
Copy link
Collaborator Author

aboutlo commented Nov 29, 2019

Not sure if I get what you mean.
Are you referring to S.extend(Base).id('extended') case?
the issue is in how we add an attribute.

const setAttribute = ({ schema, ...options }, attribute) => {
  const [key, value] = attribute
  const currentProp = last(schema.properties)
  if (currentProp) {
    const { name, ...props } = currentProp
    return options.factory({ schema, ...options }).prop(name, {
      [key]: value,
      ...props,
    })
  }
  return options.factory({ schema: { ...schema, [key]: value }, ...options })

currentProp is the last prop added so the id will be added to the last prop added in the base not at the root level as the syntax let you imagine.

In order to fix that I had to change the order:

const base = S.object().id('base')
S.object().id('extended').extend(base)

With this approach extend can merge the extended and base schemas in the right order

 const extended = merge(state, schema)

https://github.com/fastify/fluent-schema/pull/54/files#diff-6882624535de7cb6b7bae3a1912c1b18R258

@aboutlo aboutlo requested a review from mcollina December 1, 2019 18:39
@aboutlo
Copy link
Collaborator Author

aboutlo commented Dec 1, 2019

notice: this is breaking change. The previous API is broken and I can't figure out a way to support the original approach S.extend(S.object().id('base').prop('foo')).id('extended').prop('bar')

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, +1 to bump the major.

@aboutlo aboutlo merged commit 7b7351f into master Dec 2, 2019
@aboutlo aboutlo deleted the root-id-not-overrided branch April 23, 2020 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants