Skip to content

Question: how to implement a basic object schema? #41

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

Closed
jsumners opened this issue Aug 9, 2019 · 7 comments
Closed

Question: how to implement a basic object schema? #41

jsumners opened this issue Aug 9, 2019 · 7 comments
Labels
bug Something isn't working

Comments

@jsumners
Copy link
Member

jsumners commented Aug 9, 2019

Basically, I want to implement this test with fluent-schema -- https://github.com/fastify/fast-json-stringify/blob/06acd3bf09a6c0136eb442d0d95e2a5b1fd85cf2/test/additionalProperties.test.js#L68-L79

My attempt keeps resulting in various errors -- https://runkit.com/embed/ep1pfme4238t

var fastJson = require('fast-json-stringify')
var fluent = require("fluent-schema")
var schema = fluent.object().prop(
    'error',
    fluent.object().additionalProperties(true).prop('message')
)
console.log(schema.valueOf())
var stringify = fastJson(schema.valueOf())
stringify({ error: { message: 'foo', code: 'internal_server_error' } })
@aboutlo
Copy link
Collaborator

aboutlo commented Aug 9, 2019

I don't know how it works fastJson in details but I have the feeling the issue could be there

to me

{
    title: 'check string coerce',
    type: 'object',
    properties: {},
    additionalProperties: true
  }

become

const schema = S.object().additionalProperties(true).title('title string coerce')
console.log(JSON.stringify(schema.valueOf(),undefined,2))

which will generate

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "additionalProperties": {},
  "title": "title string coerce"
}

I wonder about additionalProperties: true it isn't required to be set as true because by default a schema accepts an object with extra props. additionalProperties: true will be translated as additionalProperties: {}

So either you set to false or you pass a schema to specify which kind of extra props you want.

@jsumners
Copy link
Member Author

jsumners commented Aug 9, 2019

Without additionalProperties:true fast-json-stringify will strip properties that are not explicitly stated in the schema.

@aboutlo
Copy link
Collaborator

aboutlo commented Aug 12, 2019

So looking to the reference:
https://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.6

The value of "additionalProperties" MUST be a valid JSON Schema.

So the bug is here: https://github.com/fastify/fluent-schema/blob/73adbf43075104d5ac05a874313fd7dbbda72474/src/ObjectSchema.js#L52-L52

we should need something like this:

additionalProperties: value => {
      if (typeof value !== 'boolean' && !isFluentSchema(value))
        throw new Error("'additionalProperties' must be a boolean or a FluentSchema object")
      const attribute =
        typeof value === 'boolean'
          ? value.valueOf()
          : omit(value.valueOf(), ['$schema'])
      return setAttribute({ schema, ...options }, [
        'additionalProperties',
        attribute,
        'array',
      ])
    }

@aboutlo aboutlo added the bug Something isn't working label Aug 12, 2019
@jsumners
Copy link
Member Author

FYI: re-reading this issue this morning I noticed that I left out a "not" in my last reply. Please re-read it to make sure it still makes sense.

@aboutlo
Copy link
Collaborator

aboutlo commented Aug 12, 2019

Still, a bug in fluent because of the spec

given

const schema = S.object().additionalProperties(true).title('title string coerce')

expect:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "additionalProperties": true,
  "title": "title string coerce"
}

rather than:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "additionalProperties": {},
  "title": "title string coerce"
}

However, additionalProperties: {} is valid it means don't strip
I found somewhere else where they are discussing the same topic: OAI/OpenAPI-Specification#668 (comment)

@aboutlo
Copy link
Collaborator

aboutlo commented Aug 26, 2019

PR merged

@aboutlo aboutlo closed this as completed Aug 26, 2019
@jsumners
Copy link
Member Author

Thank you.

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

No branches or pull requests

2 participants