diff --git a/package-lock.json b/package-lock.json index 084dec9..c7af7af 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/resource", - "version": "5.4.0", + "version": "5.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/resource", - "version": "5.4.0", + "version": "5.5.0", "license": "MIT", "devDependencies": { "@athenna/artisan": "^5.7.0", diff --git a/package.json b/package.json index 67501a6..f592331 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/resource", - "version": "5.4.0", + "version": "5.5.0", "description": "Add transformation layers between your application data transfer.", "license": "MIT", "author": "João Lenon ", diff --git a/src/resource/BaseResource.ts b/src/resource/BaseResource.ts index 3cb2f0e..10b850d 100644 --- a/src/resource/BaseResource.ts +++ b/src/resource/BaseResource.ts @@ -33,10 +33,25 @@ export abstract class BaseResource { */ private readonly isArray: boolean + /** + * Indicates if the resource should be automatically + * transformed to JSON when constructed. If set to `true`, + * when you construct the resource it will automatically call + * the `toJSON()` method to validate that the schema is valid. + * + * This is useful when you want to fail fast if the schema is not + * valid. + */ + protected autoValidateSchema = true + public constructor(data: I | I[]) { this.defaultValues = [] this.isArray = Is.Array(data) this.items = this.isArray ? (data as I[]) : [data as I] + + if (this.autoValidateSchema) { + this.toJSON() + } } /**