Skip to content

Commit

Permalink
Add a default validate method on Model
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbrand committed Nov 14, 2023
1 parent 0cc6669 commit 39c47a8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) || {};

Expand Down Expand Up @@ -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.
*/
Expand Down
9 changes: 7 additions & 2 deletions src/types/model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {};
/**
Expand Down Expand Up @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/types/model.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 39c47a8

Please sign in to comment.