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 #3265

Closed
wants to merge 7 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
22 changes: 22 additions & 0 deletions docs/site/Model.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,28 @@ 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.

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 @@ -403,4 +414,13 @@ describe('model', () => {
class MyModel extends Entity {}
expect(MyModel.modelName).to.equal('MyModel');
});

it('hidden password from userModel', () => {
frbuceta marked this conversation as resolved.
Show resolved Hide resolved
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: Array<string> = def.settings.hiddenProperties || []; // ToDo (frbuceta): Short-term solution
frbuceta marked this conversation as resolved.
Show resolved Hide resolved
for (const p in def.properties) {
if (p in this) {
if (p in this && hiddenProperties.indexOf(p) === -1) {
copyPropertyAsJson(p);
}
}
Expand Down