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

Required fields support #2

Merged
merged 1 commit into from
Aug 19, 2016
Merged

Conversation

delvedor
Copy link
Member

Added required field support.

Example:

const fastJson = require('fast-json-stringify')
const stringify = fastJson({
  title: 'schema with required field',
  type: 'object',
  properties: {
    str: {
      type: 'string',
      required: true
    },
    num: {
      type: 'number'
    }
  }
})

If the required field is not provided, fast-json-stringify will throw an error.

Required feedback 😄

if (!obj.hasOwnProperty('${key.slice(1)}')) {
throw new Error('${key} is required!')
}`
}
Copy link
Member

Choose a reason for hiding this comment

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

what happens if a key is not required, but it is not present?

Copy link
Member Author

Choose a reason for hiding this comment

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

This:

const fastJson = require('fast-json-stringify')
const stringify = fastJson({
  title: 'schema with required field',
  type: 'object',
  properties: {
    str: {
      type: 'string',
      required: true
    },
    num: {
      type: 'number'
    }
  }
})

console.log(stringify({
  str: 'string'
}) // '{"str":"string","num":null}'

@mcollina
Copy link
Member

Good job, one nit :)

@mcollina mcollina merged commit 12a6308 into fastify:master Aug 19, 2016
@mcollina
Copy link
Member

THx! :)

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.

None yet

2 participants