Skip to content

Commit

Permalink
fix: hidden properties when strict is false
Browse files Browse the repository at this point in the history
  • Loading branch information
frbuceta authored and raymondfeng committed May 18, 2020
1 parent c2817e3 commit 133cc6b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
14 changes: 13 additions & 1 deletion packages/repository/src/__tests__/unit/model/model.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ describe('model', () => {
const flexibleDef = new ModelDefinition('Flexible');
flexibleDef
.addProperty('id', {type: 'string', id: true})
.addProperty('createdAt', Date)
.addSetting('hiddenProperties', ['createdAt'])
.addSetting('strict', false);

const addressDef = new ModelDefinition('Address');
Expand Down Expand Up @@ -121,8 +123,8 @@ describe('model', () => {

class Flexible extends Entity {
static definition = flexibleDef;

id: string;
createdAt?: Date;

constructor(data?: Partial<Flexible>) {
super(data);
Expand Down Expand Up @@ -460,6 +462,16 @@ describe('model', () => {
});
});

it('excludes hidden properties from toJSON() output with strict false', () => {
const flexible = new Flexible({
id: '123a',
createdAt: new Date(),
});
expect(flexible.toJSON()).to.eql({
id: '123a',
});
});

describe('rejectNavigationalPropertiesInData', () => {
class Order extends Entity {
static definition = new ModelDefinition('Order')
Expand Down
9 changes: 6 additions & 3 deletions packages/repository/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,20 @@ export abstract class Model {
* See function `asObject` for each property's conversion rules.
*/
toObject(options?: Options): Object {
const def = (this.constructor as typeof Model).definition;
const obj: AnyObject = {};

if (options && options.ignoreUnknownProperties === false) {
const hiddenProperties: string[] = def?.settings.hiddenProperties || [];
for (const p in this) {
const val = (this as AnyObject)[p];
obj[p] = asObject(val, options);
if (!hiddenProperties.includes(p)) {
const val = (this as AnyObject)[p];
obj[p] = asObject(val, options);
}
}
return obj;
}

const def = (this.constructor as typeof Model).definition;
const props = def.properties;
const keys = Object.keys(props);

Expand Down

0 comments on commit 133cc6b

Please sign in to comment.