Repository: fastify/fast-json-stringify (JavaScript)

Issue:
Stringify fails with multiple defaults
<!--
Before you submit an issue we recommend you visit [Fastify Help](https://github.com/fastify/help) and ask any questions you have or mention any problems you've had getting started with Fastify.

**Please read this entire template before posting any issue. If you ignore these instructions
and post an issue here that does not follow the instructions, your issue might be closed,
locked, and assigned the `missing discussion` label.**
-->

## 🐛 Bug Report

Stringifying fails when using multiple defaults + required.  This isn't the case when using AJV directly.  
You can run it here: https://repl.it/@gabemeola/fast-json-stringify-default-issue  

Removing `required` passes but isn't the intended schema.  
Removing all but one `default` and `required` does pass as intended. 
e.g.  
```
const schema = {
  type: 'object',
  properties: {
    name: {
      default: 'Person',
      type: 'string',
    },
  },
  required: [
    'name',
  ]
}

```

## To Reproduce

Steps to reproduce the behavior:

```js
const fastJson = require('fast-json-stringify')
const Ajv =  require('ajv')

const ajv = new Ajv({ useDefaults: true });

const schema = {
  type: 'object',
  properties: {
    name: {
      default: 'Person',
      type: 'string',
    },
    dev: {
      type: 'boolean',
      default: false,
    }
  },
  required: [
    'name',
    'dev'
  ]
}

const validate = ajv.compile(schema);
const str = fastJson(schema)

console.log("AJV Passed:", validate({}))
console.log(str({})) // Error: "name" is required!
```

## Expected behavior

A clear and concise description of what you expected to happen.

```js
{ "name": "Person", "dev": false }
```

## Your Environment

- *node version*: 12
- *fastify version*: 2.2.3



Discussion:
Yeah, it is definitely a [bug](https://runkit.com/embed/9n39lo3v797d)!

Would you like to send a Pull Request to address this issue?
Sure thing, I can look into this later this weekend!



Patch:
diff --git a/index.js b/index.js
index e6bbbd0f..6f40fde4 100644
--- a/index.js
+++ b/index.js
@@ -755,14 +755,14 @@ function buildCode (location, code, laterCode, name) {
 
     var defaultValue = schema.properties[key].default
     if (defaultValue !== undefined) {
-      required = filterRequired(schema.required, key)
+      required = filterRequired(required, key)
       code += `
       } else {
         ${addComma}
         json += ${asString} + ':' + ${JSON.stringify(JSON.stringify(defaultValue))}
       `
-    } else if (schema.required && schema.required.indexOf(key) !== -1) {
-      required = filterRequired(schema.required, key)
+    } else if (required && required.indexOf(key) !== -1) {
+      required = filterRequired(required, key)
       code += `
       } else {
         throw new Error('${sanitized} is required!')


Test patch:
diff --git a/test/defaults.test.js b/test/defaults.test.js
index dc1509c2..dfb5d3c6 100644
--- a/test/defaults.test.js
+++ b/test/defaults.test.js
@@ -337,3 +337,40 @@ buildTest({
 }, {
   level1: { level2: { level3: { level4: { } } } }
 })
+
+buildTest({
+  type: 'object',
+  properties: {
+    name: {
+      type: 'string',
+      default: 'foo'
+    },
+    dev: {
+      type: 'boolean',
+      default: false
+    }
+  },
+  required: [
+    'name', 'dev'
+  ]
+}, {}, { name: 'foo', dev: false })
+
+buildTest({
+  type: 'object',
+  properties: {
+    name: {
+      type: 'string',
+      default: 'foo'
+    },
+    dev: {
+      type: 'boolean'
+    },
+    job: {
+      type: 'string',
+      default: 'awesome'
+    }
+  },
+  required: [
+    'name', 'dev'
+  ]
+}, { dev: true }, { name: 'foo', dev: true, job: 'awesome' })