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

Add Short-term solution for hiddenProperties in model #3304

Merged
merged 1 commit into from
Jul 19, 2019
Merged
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
25 changes: 25 additions & 0 deletions docs/site/Model.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,31 @@ Additionally, the model decorator is able to build the properties object through
the information passed in or inferred by the property decorators, so the
properties key value pair can also be omitted.

#### Hidden properties

The properties are stored in the database, available in JS/TS code, can be set
via POST/PUT/PATCH requests, but they are removed from response bodies
(`.toJSON()` output).

To hide a property, you can use the `hiddenProperties` setting like this:

```ts
@model({
settings: {
hiddenProperties: ['password']
}
})
class MyUserModel extends Entity {
@property({id: true})
id: number;
@property({type: 'string'})
email: string;
@property({type: 'string'})
password: string;
...
}
```

### Property Decorator

The property decorator takes in the same arguments used in LoopBack 3 for
Expand Down
24 changes: 22 additions & 2 deletions packages/repository/src/__tests__/unit/model/model.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ describe('model', () => {
userDef
.addProperty('id', {type: 'string', id: true})
.addProperty('email', 'string')
.addProperty('password', 'string')
.addProperty('firstName', String)
.addProperty('lastName', STRING);
.addProperty('lastName', STRING)
.addSetting('hiddenProperties', ['password']);

const flexibleDef = new ModelDefinition('Flexible');
flexibleDef
Expand Down Expand Up @@ -101,11 +103,11 @@ describe('model', () => {
}
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
class User extends Entity {
static definition = userDef;
id: string;
email: string;
password: string;
firstName: string;

constructor(data?: Partial<User>) {
Expand Down Expand Up @@ -154,6 +156,15 @@ describe('model', () => {
return customer;
}

function createUser() {
const user = new User();
user.id = '123';
user.email = '[email protected]';
user.password = '1234test';
user.firstName = 'Test User';
return user;
}

it('adds properties', () => {
expect(customerDef.name).to.eql('Customer');
expect(customerDef.properties).have.properties(
Expand Down Expand Up @@ -408,4 +419,13 @@ describe('model', () => {
class MyModel extends Entity {}
expect(MyModel.modelName).to.equal('MyModel');
});

it('excludes hidden properties from toJSON() output', () => {
const user = createUser();
expect(user.toJSON()).to.eql({
id: '123',
email: '[email protected]',
firstName: 'Test User',
});
});
});
3 changes: 2 additions & 1 deletion packages/repository/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ export abstract class Model {
};

const json: AnyObject = {};
const hiddenProperties: string[] = def.settings.hiddenProperties || [];
for (const p in def.properties) {
if (p in this) {
if (p in this && !hiddenProperties.includes(p)) {
copyPropertyAsJson(p);
}
}
Expand Down