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

Set $id after clone #193

Closed
2 tasks done
Eomm opened this issue Sep 15, 2022 · 7 comments
Closed
2 tasks done

Set $id after clone #193

Eomm opened this issue Sep 15, 2022 · 7 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@Eomm
Copy link
Member

Eomm commented Sep 15, 2022

Prerequisites

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

Issue

Given an origin flunet-json-schema object filtered with only/without it is not possible to set the BaseSchema's $id:

const WhizBang = fluent
  .object()
  .additionalProperties(false)
  .id('whizbang')
  .title('WhizBang')
  .prop(
    'whiz'
  , fluent.string().required().minLength(1)
  )
  .prop(
    'bang'
  , fluent.string().required().minLength(1)
  )

const part = WhizBang.only(['whiz'])
console.log(JSON.stringify(part.id('asd').valueOf(), null, 2))

Shows:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "additionalProperties": false,
  "$id": "whizbang",
  "title": "WhizBang",
  "properties": {
    "whiz": {
+      "$id": "asd",
      "type": "string",
      "minLength": 1
    }
  },
  "required": [
    "whiz"
  ]
}

I can't find a way to set the "$id": "whizbang" field.

Ref fastify/fastify#4276

@zrosenbauer
Copy link

@Eomm instead of resetting the ID would it make sense to just omit it? I'm not sure it should even reference the $id at this point...

.only & .without are state mutations that invalidate the ref aka should compose new objects not mutate the old?

@esatterwhite
Copy link
Contributor

It would need to be both. It would need to omit the id by default, and allow you to set it in the correct location of the schema

@zrosenbauer
Copy link

zrosenbauer commented Sep 19, 2022

I'm happy to open up a PR handling both if @Eomm or other core folks thinks it make sense.

@Eomm
Copy link
Member Author

Eomm commented Sep 20, 2022

Yes, I agree:

  • remove it by default
  • allow to set it again by calling .id()

Note that this would be a breaking change

@mcollina mcollina added bug Something isn't working good first issue Good for newcomers labels Sep 20, 2022
@zrosenbauer
Copy link

Ok cool, I'm gonna take a stab this weekend or if I get some time after hours. 🚀

@esatterwhite
Copy link
Contributor

The problem is rooted in the use of setAttribute of BaseSchema

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

Once an object has properties, all things are added to its properties rather than the root of the schema. this doesn't seem too hard to fix

@aboutlo
Copy link
Collaborator

aboutlo commented Oct 3, 2022

resolved by #195

@aboutlo aboutlo closed this as completed Oct 3, 2022
This issue was closed.
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

5 participants