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

TypeCoercion like ajv #607

Open
2 tasks done
Uzlopak opened this issue Feb 24, 2023 · 5 comments
Open
2 tasks done

TypeCoercion like ajv #607

Uzlopak opened this issue Feb 24, 2023 · 5 comments

Comments

@Uzlopak
Copy link
Contributor

Uzlopak commented Feb 24, 2023

Prerequisites

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

Issue

In my opinion, we should do the same type coercion like ajv does.

https://ajv.js.org/coercion.html

@jsumners
Copy link
Member

To what end? AJV is coercing JSON into JavaScript natives. FJS is converting JavaScript natives into JSON.

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Feb 24, 2023

const fastJson = require('fast-json-stringify')

const stringify = fastJson({
  title: 'Example Schema',
  type: 'object',
  properties: {
    boolean: {
      type: 'boolean'
    }
    }
})

console.log(stringify({
    boolean: "false"
}))

results in "{\"boolean\":true}"

awesome

@ivan-tymoshenko
Copy link
Member

ivan-tymoshenko commented Feb 24, 2023

Not a good example IMHO. With the same result, you can complain about the js standard.
!!"false" === true

but I also don't like some types coercions that we have. The question is what should we do in this case?

  • throw an error
  • return { boolean: "false" }
  • return { boolean: false }
  • return { boolean: true }
    return {}

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Feb 24, 2023

The risk that I see here is about diverging behaviour between ajv and fsj. Then we have some cases were ajv validates as valid and fsj maybe throws or serializes it differently. Also we potentially need to coerce the vaues if we want to process them for stuff like const and enum. If we want to go that way.

@Eomm
Copy link
Member

Eomm commented Feb 26, 2023

I'm pro to good coercion default and not throwing.

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

No branches or pull requests

4 participants