Skip to content

Required fields support#2

Merged
mcollina merged 1 commit into
fastify:masterfrom
delvedor:required-field
Aug 19, 2016
Merged

Required fields support#2
mcollina merged 1 commit into
fastify:masterfrom
delvedor:required-field

Conversation

@delvedor
Copy link
Copy Markdown
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 😄

Comment thread index.js
if (!obj.hasOwnProperty('${key.slice(1)}')) {
throw new Error('${key} is required!')
}`
}
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Good job, one nit :)

@mcollina mcollina merged commit 12a6308 into fastify:master Aug 19, 2016
@mcollina
Copy link
Copy Markdown
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.

2 participants