Skip to content

Commit

Permalink
Merge pull request #5 from janis-commerce/add-postStructValidate-method
Browse files Browse the repository at this point in the history
Add post struct validate method
  • Loading branch information
jormaechea committed Dec 10, 2019
2 parents 53373cc + ef22ba6 commit 935c1a7
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 3 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
1 change: 1 addition & 0 deletions lib/api-save-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ApiSaveError extends Error {
return {
INVALID_REQUEST_DATA: 1,
INVALID_ENTITY: 2,
VALIDATION_ERROR: 3,
INTERNAL_ERROR: 99
};

Expand Down
2 changes: 1 addition & 1 deletion lib/api-save-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ApiSaveValidator {
} catch(e) {
const errorPath = e.path.join('.');
e.message = `${e.message} in ${errorPath}`;
throw new ApiSaveError(e, ApiSaveError.codes.INVALID_REQUEST_DATA);
throw new ApiSaveError(e, ApiSaveError.codes.VALIDATION_ERROR);
}
}

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.resolves('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 fail if postStructValidate 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 935c1a7

Please sign in to comment.