Skip to content

Commit

Permalink
Merge pull request #12 from fashion-js/11-individual-property-clean
Browse files Browse the repository at this point in the history
Fixes #11 - Allow individual properties to implement a clean function.
  • Loading branch information
austinkelleher committed Jan 16, 2018
2 parents c425e7d + e12140e commit 75a4090
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,15 @@ Model_proto.clean = function (options) {
}
} else {
const propertyType = property.type;
const propertyClean = property.clean;
const clean = propertyType.clean;
const oldProperty = options.property;
options.property = property;

if (clean) {
if (propertyClean) {
// call the clean method on the property
value = propertyClean(value, options);
} else if (clean) {
// call the clean function provided by model
value = propertyType.clean(value, options);
} else if (value.Model || propertyType.isWrapped()) {
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,32 @@ assert(typeof cleanedImage.data.constructor === 'string');
console.log(cleanedImage.data);
```

Implementing a `clean` function on individual properties is also supported:

```js
const Person = Model.extend({
properties: {
name: String,
ssn: {
type: String,
clean: function (value, options) {
// Only return the value of the ssn property if the `showSensitive`
// option is passed to `clean`.
return (options.showSensitive) ? value : undefined;
}
}
}
});

const person = new Person({ name: 'John', ssn: 'abc123' });

assert.deepEqual(person.clean(), { name: 'John' });
assert.deepEqual(person.clean({ showSensitive: true }), {
name: 'John',
ssn: 'abc123'
});
```

### Stringify

Model instances have a `stringify` function that can be used to
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
"dependencies": {
"events": "^1.1.1"
},
"version": "6.0.2"
}
"version": "6.1.0"
}
52 changes: 52 additions & 0 deletions test/clean.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,3 +756,55 @@ test('should allow enum values to override clean in static method', function (t)
t.is(Model.clean(Gender.MALE), 'M');
t.is(Model.clean(Gender.FEMALE), 'F');
});

test('should allow clean function on individual properties', function (t) {
const Person = Model.extend({
properties: {
name: String,
ssn: {
type: String,
clean: function (val, options) {
t.is(val, 'abc123');
if (options.showSensitive) return val;
}
}
}
});

const person = Person.wrap({
name: 'John',
ssn: 'abc123'
});

t.deepEqual(person.clean(), { name: 'John' });
t.deepEqual(person.clean({ showSensitive: true }), {
name: 'John',
ssn: 'abc123'
});
});

test('should allow clean function on individual properties when using Model.clean', function (t) {
const Person = Model.extend({
properties: {
name: String,
ssn: {
type: String,
clean: function (val, options) {
t.is(val, 'abc123');
if (options.showSensitive) return val;
}
}
}
});

const person = Person.wrap({
name: 'John',
ssn: 'abc123'
});

t.deepEqual(Model.clean(person), { name: 'John' });
t.deepEqual(Model.clean(person, { showSensitive: true }), {
name: 'John',
ssn: 'abc123'
});
});

0 comments on commit 75a4090

Please sign in to comment.