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

Skip serialization / deserialization #32

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ Can be used to create simple custom propSchema.
- `serializer` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** function that takes a model value and turns it into a json value
- `deserializer` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** function that takes a json value and turns it into a model value

The `serializer` or `deserializer` functions may return `SKIP` to indicate that the property should not be used. This can be usefull for objects who's state affects whether they should be saved or updated.

**Examples**

```javascript
Expand All @@ -592,9 +594,13 @@ var schema = _.createSimpleSchema({
function(v) { return v + 2 },
function(v) { return v - 2 }
)
b: _.custom(_.custom(
function(v) { return v },
function(v) { return v === 'INVALID' ? _.SKIP : v }
)
})
t.deepEqual(_.serialize(s, { a: 4 }), { a: 6 })
t.deepEqual(_.deserialize(s, { a: 6 }), { a: 4 })
t.deepEqual(_.serialize(s, { a: 4, b: 'INVALID' }), { a: 6, b: 'INVALID' })
t.deepEqual(_.deserialize(s, { a: 6 }), { a: 4 }) // no "b" is included because it's 'INVALID'
```

Returns **PropSchema**
Expand Down
16 changes: 12 additions & 4 deletions serializr.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(function(g) {
"use strict"

function mrFactory() {
// Indicate properties should be skipped
var SKIP = Symbol('SKIP')
Copy link
Member

Choose a reason for hiding this comment

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

Note that Symbol is not standard ES5, so something like typeof Symbol !== "undefined" ? Symbol('SKIP') : { SKIP: true } would be better. I'll try to merge later today, so I might process that change myself

/*
* Generic utility functions
*/
Expand Down Expand Up @@ -402,7 +404,10 @@
propDef = _defaultPrimitiveProp
if (propDef === false)
return
var jsonValue = propDef.serializer(obj[key])
var jsonValue = propDef.serializer(obj[key], key, obj)
if (jsonValue === SKIP){
return
}
res[propDef.jsonname || key] = jsonValue
})
return res
Expand Down Expand Up @@ -491,7 +496,9 @@
// this allows props to complete after completing the object itself
// enabling reference resolving and such
context.rootContext.createCallback(function (value) {
target[propName] = value
if (value !== SKIP){
target[propName] = value
}
}),
context,
target[propName] // initial value
Expand Down Expand Up @@ -1093,7 +1100,8 @@
child: object, // deprecate
reference: reference,
ref: reference, // deprecate
custom: custom
custom: custom,
SKIP: SKIP
}
}

Expand Down
37 changes: 37 additions & 0 deletions test/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,43 @@ test("it should respect custom schemas", t => {
t.end()
})

test("it should not set values for custom serializers/deserializer that return SKIP", t => {
t.equal(typeof _.SKIP, 'symbol')
var s = _.createSimpleSchema({
a: _.custom(
function(v) { return v },
function(v) { return _.SKIP }
)
})

t.deepEqual(_.serialize(s, { a: 4 }), { a: 4 })
t.deepEqual(_.deserialize(s, { a: 4 }), { })

s = _.createSimpleSchema({
a: _.custom(
function(v) { return _.SKIP },
function(v) { return undefined }
)
})
t.deepEqual(_.serialize(s, { a: 4 }), { })
t.deepEqual(_.deserialize(s, { a: 4 }), { a: undefined })

t.end()
})

test("it should pass key and object to custom schemas", t => {
var s = _.createSimpleSchema({
a: primitive(),
b: _.custom(
function(v, k, obj) { return k + String(v * obj.b) },
function(v) { return v }
)
})
t.deepEqual(_.serialize(s, { a: 2, b: 4 }), { a: 2, b: 'b16' })
t.deepEqual(_.deserialize(s, { a: 6, b: 2 }), { a: 6, b: 2 })
t.end()
})

test("it should respect extends", t => {
var superSchema = _.createSimpleSchema({
x: primitive()
Expand Down