| 1 | /* |
| 2 | Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/ |
| 3 | |
| 4 | This program is free software; |
| 5 | you can redistribute it and/or modify it under the terms of the |
| 6 | GNU General Public License as published by the Free Software Foundation; |
| 7 | either version 3 of the License, or any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | /* |
| 19 | Changes: |
| 20 | - v4.0.0: |
| 21 | - created from v3.6.0 |
| 22 | Doc reviewed 202208 |
| 23 | */ |
| 24 | |
| 25 | import TravelUpdater from '../data/TravelUpdater.js'; |
| 26 | |
| 27 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 28 | /** |
| 29 | This class is the bases class for all the objects contained in a travel |
| 30 | */ |
| 31 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 32 | |
| 33 | class TravelObject { |
| 34 | |
| 35 | /** |
| 36 | The constructor |
| 37 | */ |
| 38 | |
| 39 | constructor ( ) { |
| 40 | Object.freeze ( this ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | Verify that the parameter can be transformed to a TravelObject |
| 45 | @param {JsonObject} something an object to validate |
| 46 | @return {JsonObject} the validated object |
| 47 | */ |
| 48 | |
| 49 | validateObject ( something ) { |
| 50 | if ( ! Object.getOwnPropertyNames ( something ).includes ( 'objType' ) ) { |
| 51 | throw new Error ( 'No objType for ' + this.objType.name ); |
| 52 | } |
| 53 | this.objType.validate ( something.objType ); |
| 54 | if ( 'Travel' === this.objType.name && this.objType.version !== something.objType.version ) { |
| 55 | new TravelUpdater ( ).update ( something ); |
| 56 | } |
| 57 | const properties = Object.getOwnPropertyNames ( something ); |
| 58 | this.objType.properties.forEach ( |
| 59 | property => { |
| 60 | if ( ! properties.includes ( property ) ) { |
| 61 | throw new Error ( 'No ' + property + ' for ' + this.objType.name ); |
| 62 | } |
| 63 | } |
| 64 | ); |
| 65 | return something; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | export default TravelObject; |
| 70 | |
| 71 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
| 72 |