Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Plugin Validators Not Being Registered #119

Open
CatGuardian opened this issue Mar 31, 2018 · 0 comments
Open

Bug: Plugin Validators Not Being Registered #119

CatGuardian opened this issue Mar 31, 2018 · 0 comments
Assignees
Labels
bug This issue involves a bug in the library's behaviour and will result in a patch release
Milestone

Comments

@CatGuardian
Copy link

Hi Benjamin,

A long while back I asked you about how to automatically do a createdAt and updatedAt property for all of my Models and you suggested a Plugin that looks something like the following (the point is to look at the public validate portion):

import { Changes, Instance, Model, Plugin } from 'iridium';
import * as Skmatc from 'skmatc';

export class CreatedUpdatedPlugin implements Plugin {
  public newModel(model: Model<any, any>): void {
    Object.assign(model.schema, {
      createdAt: 'readonly',
      updatedAt: 'readonly',
    });

    const oldOnCreating = model.hooks.onCreating;
    model.hooks.onCreating = async (doc: any) => {
      oldOnCreating && await Promise.resolve(oldOnCreating(doc));

      doc.createdAt = new Date();
      doc.updatedAt = new Date();
    };

    const oldOnSaving = model.hooks.onSaving;
    model.hooks.onSaving = async (instance: Instance<any, any>, changes: Changes) => {
      oldOnSaving && await Promise.resolve(oldOnSaving(instance, changes));

      if (changes) {
        changes.$currentDate = Object.assign(changes.$currentDate || {}, {
          updatedAt: true,
        });
      }
    };
  }

  public validate = [
    Skmatc.create((schema) => {
      return schema === 'readonly';
    }, function (schema, data, path) {
      return this.fail('no changes allowed');
    }),
  ];
}

But it turns out that the public validate thing isn't being utilized by Iridium anywhere.

I'll have a Pull Request out shortly.

@notheotherben notheotherben added this to the Version 8 milestone Apr 1, 2018
@notheotherben notheotherben added the bug This issue involves a bug in the library's behaviour and will result in a patch release label Apr 1, 2018
@notheotherben notheotherben self-assigned this Apr 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue involves a bug in the library's behaviour and will result in a patch release
Projects
None yet
Development

No branches or pull requests

2 participants