Skip to content

Commit 8287e68

Browse files
authored
Merge pull request #3 from mcollina/missing-values
Support for missing parameters
2 parents a6f7292 + 09624d5 commit 8287e68

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ And nested ones, too.
7474
| `Date` | `string` via `toISOString()` |
7575
| `RegExp` | `string` |
7676

77-
#### Required
77+
#### Required
7878
You can set specific fields of an object as `required` in your schema, by adding `required: true` inside the key properties.
7979
Example:
8080
```javascript
@@ -94,6 +94,31 @@ const schema = {
9494
```
9595
If the object to stringify has not the required field(s), `fast-json-stringify` will throw an error.
9696

97+
#### Missing fields
98+
If a field *is present* in the schema (and is not required) but it *is not present* in the object to stringify, `fast-json-stringify` will not write it in the final string.
99+
Example:
100+
```javascript
101+
const stringify = fastJson({
102+
title: 'Example Schema',
103+
type: 'object',
104+
properties: {
105+
nickname: {
106+
type: 'string'
107+
},
108+
mail: {
109+
type: 'string',
110+
required: true
111+
}
112+
}
113+
})
114+
115+
const obj = {
116+
117+
}
118+
119+
console.log(stringify(obj)) // '{"mail":"[email protected]"}'
120+
```
121+
97122
## Acknowledgements
98123

99124
This project was kindly sponsored by [nearForm](http://nearform.com).

index.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,34 @@ function buildObject (schema, code, name) {
140140
var laterCode = ''
141141

142142
Object.keys(schema.properties).forEach((key, i, a) => {
143+
// Using obj.key !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
144+
// see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.
143145
code += `
144-
json += '${$asString(key)}:'
145-
`
146+
if (obj.${key} !== undefined) {
147+
json += '${$asString(key)}:'
148+
`
146149

147150
const result = nested(laterCode, name, '.' + key, schema.properties[key])
148151

149152
code += result.code
150153
laterCode = result.laterCode
151154

152155
if (i < a.length - 1) {
153-
code += 'json += \',\''
156+
code += `
157+
json += \',\'
158+
`
159+
}
160+
161+
if (schema.properties[key].required) {
162+
code += `
163+
} else {
164+
throw new Error('${key} is required!')
165+
`
154166
}
167+
168+
code += `
169+
}
170+
`
155171
})
156172

157173
code += `
@@ -202,12 +218,6 @@ function buildArray (schema, code, name) {
202218
function nested (laterCode, name, key, schema) {
203219
var code = ''
204220
var funcName
205-
if (schema.required) {
206-
code += `
207-
if (!obj.hasOwnProperty('${key.slice(1)}')) {
208-
throw new Error('${key} is required!')
209-
}`
210-
}
211221
const type = schema.type
212222
switch (type) {
213223
case 'null':

test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,31 @@ test('object with required field', (t) => {
288288
})
289289
t.fail()
290290
} catch (e) {
291-
t.is(e.message, '.str is required!')
291+
t.is(e.message, 'str is required!')
292292
t.pass()
293293
}
294294
})
295+
296+
test('missing values', (t) => {
297+
t.plan(3)
298+
299+
const stringify = build({
300+
title: 'object with missing values',
301+
type: 'object',
302+
properties: {
303+
str: {
304+
type: 'string'
305+
},
306+
num: {
307+
type: 'number'
308+
},
309+
val: {
310+
type: 'string'
311+
}
312+
}
313+
})
314+
315+
t.equal('{"val":"value"}', stringify({ val: 'value' }))
316+
t.equal('{"str":"string","val":"value"}', stringify({ str: 'string', val: 'value' }))
317+
t.equal('{"str":"string","num":42,"val":"value"}', stringify({ str: 'string', num: 42, val: 'value' }))
318+
})

0 commit comments

Comments
 (0)