@janiscommerce/api-save
Advanced tools
Comparing version 2.2.0 to 3.0.0
@@ -10,2 +10,6 @@ # Changelog | ||
## [3.0.0] - 2019-08-01 | ||
### Changed | ||
- Now the validation struct is defined as 3 optional getters (BREAKING CHANGE). See README for new documentation. | ||
## [2.2.0] - 2019-07-31 | ||
@@ -12,0 +16,0 @@ ### Changed |
'use strict'; | ||
const { API } = require('@janiscommerce/api'); | ||
const { struct } = require('superstruct'); | ||
@@ -17,2 +18,14 @@ const ApiSaveValidator = require('./api-save-validator'); | ||
static get idStruct() { | ||
return struct.optional('string|number'); | ||
} | ||
static get mainStruct() { | ||
return 'object'; | ||
} | ||
static get relationshipsStruct() { | ||
return struct.partial({}); | ||
} | ||
async validate() { | ||
@@ -19,0 +32,0 @@ |
@@ -10,7 +10,7 @@ 'use strict'; | ||
static get defaultStruct() { | ||
get struct() { | ||
return { | ||
id: struct.optional('string|number'), | ||
main: struct('object'), | ||
relationships: struct.partial({}) | ||
id: this.apiInstance.constructor.idStruct, | ||
main: this.apiInstance.constructor.mainStruct, | ||
relationships: this.apiInstance.constructor.relationshipsStruct | ||
}; | ||
@@ -25,3 +25,3 @@ } | ||
const validationStruct = this.apiInstance.getStruct ? this.apiInstance.getStruct() : this.constructor.defaultStruct; | ||
const validationStruct = this.struct; | ||
@@ -28,0 +28,0 @@ const dataToValidate = { |
{ | ||
"name": "@janiscommerce/api-save", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"description": "A package to handle JANIS Views Save APIs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -35,14 +35,18 @@ # API Save | ||
getStruct() { | ||
static get idStruct() { | ||
return struct.optional('string|number'); | ||
} | ||
const baseStruct = super.getStruct(); | ||
static get mainStruct() { | ||
return struct.partial({ | ||
name: 'string', | ||
description: 'string?', | ||
status: 'number' | ||
}); | ||
} | ||
return { | ||
...baseStruct, | ||
main: struct.partial({ | ||
name: 'string', | ||
description: 'string?', | ||
status: 'number' | ||
}) | ||
}; | ||
static get relationshipsStruct() { | ||
return struct.partial({ | ||
children: ['string'] | ||
}); | ||
} | ||
@@ -65,3 +69,3 @@ | ||
The following getters and methods can be used to customize and validate your Save API. | ||
All of them are optional, however you're encouraged to implement `getStruct()` so you don't save trash data in your DB. | ||
All of them are optional, however you're encouraged to implement `static get mainStruct()` so you don't save trash data in your DB. | ||
@@ -79,11 +83,15 @@ ### static get relationshipsParameters() | ||
### getStruct() | ||
This is used to validate the data received in the request. It **must** validate three properties: | ||
- `id`: The ID of the record to save. This should be validated as optional to allow new records to be created. | ||
- `main`: The data to save in the main entity. | ||
- `relationships`: The data to pass to the relationships. | ||
### static get idStruct() | ||
This is used to validate the ID received as path parameter. | ||
Defaults to an optional string or number. | ||
Default struct validates numeric or string optional ID, and any data received in the request is saved to the main entity. No relationships are saved. | ||
### static get mainStruct() | ||
This is used to validate the data received in the request, checking the data to be saved in the main entity. | ||
Defaults to an object with any property. | ||
### static get relationshipsStruct() | ||
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. | ||
### 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. |
17293
316
95