From 39c47a8da970f34f12be10738a1b6ecbe773e08b Mon Sep 17 00:00:00 2001 From: JC Brand Date: Tue, 14 Nov 2023 20:51:10 +0100 Subject: [PATCH] Add a default validate method on Model --- src/model.js | 11 +++++++++-- src/types/model.d.ts | 9 +++++++-- src/types/model.d.ts.map | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/model.js b/src/model.js index e2049fd..f1a8d6e 100644 --- a/src/model.js +++ b/src/model.js @@ -51,8 +51,6 @@ class Model extends EventEmitter(Object) { // The value returned during the last failed validation. this.validationError = null; - this.validate = this.validate ?? null; - if (options.collection) this.collection = options.collection; if (options.parse) attrs = this.parse(attrs, options) || {}; @@ -114,6 +112,15 @@ class Model extends EventEmitter(Object) { // eslint-disable-next-line class-methods-use-this initialize() {} + /** + * @param {object} attrs + * @param {object} [options] + * @returns {string} The validation error message + */ + validate(attrs, options) { + return ''; + } + /** * Return a copy of the model's `attributes` object. */ diff --git a/src/types/model.d.ts b/src/types/model.d.ts index dc2a76b..831bda1 100644 --- a/src/types/model.d.ts +++ b/src/types/model.d.ts @@ -65,8 +65,7 @@ export class Model extends Model_base { constructor(attributes?: Attributes, options?: ModelOptions, ...args: any[]); cid: any; attributes: {}; - validationError: any; - validate: any; + validationError: string; collection: any; changed: {}; /** @@ -99,6 +98,12 @@ export class Model extends Model_base { * initialization logic. */ initialize(): void; + /** + * @param {object} attrs + * @param {object} [options] + * @returns {string} The validation error message + */ + validate(attrs: object, options?: object): string; /** * Return a copy of the model's `attributes` object. */ diff --git a/src/types/model.d.ts.map b/src/types/model.d.ts.map index 2e1e4ec..a671e96 100644 --- a/src/types/model.d.ts.map +++ b/src/types/model.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../model.js"],"names":[],"mappings":"yBAgBa,OAAO,iBAAiB,EAAE,UAAU;yBACpC,OAAQ,MAAM,EAAE,GAAG,CAAC;sBAEpB,OAAQ,MAAM,EAAE,GAAG,CAAC;2BAGpB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ACH,UAJW,MAAM,GAAC,UAAU,QACjB,MAAM,GAAC,OAAO,YACd,OAAO,OAiEjB;IAED;;;;;OAKG;IACH,kBAFW,OAAO,WA2BjB;IAED;;;;OAIG;IACH,WAKC;IAED;;;;;OAKG;IACH,YAHW,OAAO,YACP,OAAO,WAIjB;IAED;;OAEG;IACH,iBAEC;IAED;;;OAGG;IACH,kBAFW,OAAO,WAIjB;IAED;;;;;OAKG;IACH,iBAHW,UAAU,YACV,OAAO,WASjB;CACF"} \ No newline at end of file +{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../model.js"],"names":[],"mappings":"yBAgBa,OAAO,iBAAiB,EAAE,UAAU;yBACpC,OAAQ,MAAM,EAAE,GAAG,CAAC;sBAEpB,OAAQ,MAAM,EAAE,GAAG,CAAC;2BAGpB,OAAQ,MAAM,EAAE,GAAG,CAAC;;;QAPjC;;;;;;;;;;;;WAYG;QAEH;;;;;WAKG;;;;;;;;;;;;;;AAnBH;;;;;;;;;;;;GAYG;AAEH;;;;;GAKG;AACH;IACE;;;;;OAKG;IACH,yBAHW,UAAU,YACV,YAAY,kBAyBtB;IAlBC,SAAmC;IACnC,eAAoB;IAGpB,wBAA2B;IAEH,gBAAoC;IAW5D,YAAiB;IAGnB;;OAEG;IACH,iCAEC;IAED;;OAEG;IACH,8BAEC;IARC,yBAA8B;IAUhC;;;;OAIG;IAEH,0BAEC;IAED;;;OAGG;IAEH,wBAEC;IAED;;;OAGG;IAEH,sBAAkB;IAElB;;;OAGG;IAEH,mBAAe;IAEf;;;;OAIG;IACH,gBAJW,MAAM,YACN,MAAM,GACJ,MAAM,CAIlB;IAED;;OAEG;IACH,cAEC;IAED;;;;;OAKG;IAEH,aALW,QAAQ,GAAC,QAAQ,GAAC,OAAO,GAAC,QAAQ,GAAC,MAAM,SACzC,KAAK,WACL,OAAO,OAKjB;IAED;;;OAGG;IACH,UAFW,MAAM,OAIhB;IAED,iBAEC;IAED,gBAEC;IAED,yBAEC;IAED,2BAEC;IAED,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