Skip to content

Commit

Permalink
Merge cb77dab into 53373cc
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelvilche committed Nov 28, 2019
2 parents 53373cc + cb77dab commit 037906e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [4.3.0] - 2019-11-28
### Added
- New method `postStructValidate` to add extra validation post struc validation

## [4.2.1] - 2019-11-13
### Changed
- Updated `api` dependency
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class MyApiSaveData extends ApiSaveData {
});
}

postStructValidate() {
return someAsyncTask(this.dataToSave.main);
}

format({ someField, ...restoOfTheRecord }) {
return {
...restoOfTheRecord,
Expand Down Expand Up @@ -95,6 +99,10 @@ Defaults to an object with any property.
This is used to validate the data received in the request, checking the data to be passed to the relationships.
Defaults to an object partial with no properties.

### postStructValidate()
This is used to validate the data received in the request, making additional validation even injecting data to the received data.
If it returns a Promise, it will be awaited.

### format(record)
You can use this to format your main record before it's saved. For example, mapping user friendly values to DB friendly values, add default values, etc.
If it returns a Promise, it will be awaited.
Expand Down
8 changes: 8 additions & 0 deletions lib/api-save-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ class ApiSaveData extends API {
this.dataToSave = apiSaveValidator.validateData();

this.model = apiSaveValidator.validateModel();

if(this.postStructValidate) {
try {
await this.postStructValidate();
} catch(e) {
throw new ApiSaveError(e, ApiSaveError.codes.INVALID_REQUEST_DATA);
}
}
}

async process() {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@janiscommerce/api-save",
"version": "4.2.1",
"version": "4.3.0",
"description": "A package to handle JANIS Views Save APIs",
"main": "lib/index.js",
"scripts": {
Expand Down
71 changes: 71 additions & 0 deletions tests/api-save.js
Original file line number Diff line number Diff line change
Expand Up @@ -1207,4 +1207,75 @@ describe('API Save', () => {
});
});

describe('Process with postStructValidate method', () => {

it('Should execute the postStructValidate after validate data', async () => {

class MyApiSaveWithPostStructValidate extends ApiSaveData {

async postStructValidate() {

const newField = 123;

this.dataToSave.main.newField = newField;
}
}

Model.prototype.insert.returns('15');

sinon.spy(MyApiSaveWithPostStructValidate.prototype, 'postStructValidate');

const apiSave = new MyApiSaveWithPostStructValidate();

apiSave.endpoint = '/api/some-entity';
apiSave.data = {
name: 'The name',
otherField: 'foo'
};

const validation = await apiSave.validate();

assert.strictEqual(validation, undefined);

await apiSave.process();

sinon.assert.calledOnce(MyApiSaveWithPostStructValidate.prototype.postStructValidate);
sinon.assert.calledOnce(Model.prototype.insert);

sinon.assert.calledWithExactly(Model.prototype.insert, {
name: 'The name',
otherField: 'foo',
newField: 123
});
});

it('Should execute the postStructValidate and throws a exception', async () => {

class MyApiSaveWithPostStructValidate extends ApiSaveData {

async postStructValidate() {
throw new Error('throws a nice exception');
}
}

sinon.spy(MyApiSaveWithPostStructValidate.prototype, 'postStructValidate');

const apiSave = new MyApiSaveWithPostStructValidate();

apiSave.endpoint = '/api/some-entity';
apiSave.data = {
name: 'The name',
otherField: 'foo'
};

await assert.rejects(() => apiSave.validate(), {
name: 'ApiSaveError',
code: ApiSaveError.codes.INVALID_REQUEST_DATA,
previousError: new Error('throws a nice exception')
});

sinon.assert.calledOnce(MyApiSaveWithPostStructValidate.prototype.postStructValidate);
});
});

});

0 comments on commit 037906e

Please sign in to comment.