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

Validation not called on server side #678

Open
DarthRumata opened this issue Jul 24, 2018 · 3 comments
Open

Validation not called on server side #678

DarthRumata opened this issue Jul 24, 2018 · 3 comments

Comments

@DarthRumata
Copy link

DarthRumata commented Jul 24, 2018

We have strange issue with our custom validator that checking unique field of Model. If there are other errors while validation custom validator doesn't launched at server(only on client in simulation). If we have no other validation errors so it launches on server and everything is ok.

How simulation should work? Does it start every time or there is some important condition?

Our validator:

Validator.create({
  name: 'unique',
  isValid({name, value, doc}) {
    const collection = doc.constructor.getCollection();
    let selector = {};
    selector[name] = value;
    console.log(selector);
    const foundItems = collection.find(selector);

    const count = foundItems.count();
    if (count > 1) {
      return false;
    }
    if (count === 0) {
      return true;
    }
    const foundItem = foundItems.fetch()[0];
    return foundItem._id === doc._id;
  },
  resolveError() {
    return 'uniqueViolationError';
  }
});

Our validated Model:

const InitiatorFormData = CompanyFormData.inherit({
  name: 'InitiatorFormData',
  fields: {
    identifier: {
      type: String,
      default: '',
      validators: [
        {type: 'unique'},
        {
          type: 'length',
          param: 8,
          message: 'lengthError',
        },
      ],
    },
    kind: {
      type: InitiatorKind,
      default: InitiatorKind.GENERAL,
    },
  },
  helpers: {
    getFieldNames() {
      return ['identifier', 'shortName', 'fullName', 'address', 'mainActivity', 'webSite', 'kind'];
    },
  },
});

and base Model:

const CompanyFormData = Class.create({
  name: 'CompanyFormData',
  typeField: 'type',
  collection: Companies,
  fields: {
    shortName: {
      type: String,
      default: '',
      validators: [{type: 'notEmpty'}],
    },
    fullName: {
      type: String,
      default: '',
      validators: [{type: 'notEmpty'}],
    },
    address: {
      type: Address,
      default: new Address(),
    },
    mainActivity: {
      type: String,
      default: '',
      validators: [{type: 'notEmpty'}],
    },
    webSite: {
      type: String,
      default: '',
      optional: true,
      validators: [{type: 'isValidWebUri'}],
    },
  },
});

P.S.: As temporary solution I disabled simulation and it allowed this validator work all the time.

@lukejagodzinski
Copy link
Member

Have you tried setting the stopOnFirstError flag to false? It's described in docs http://jagi.github.io/meteor-astronomy/v2#validation

@DarthRumata
Copy link
Author

Yes. It is always set to false. We have many form validations.
"company.validate({stopOnFirstError: false}, ..."

@lukejagodzinski
Copy link
Member

So, you have to create reproduction repository. Without that it's hard to tell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants