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

Incrementally add rules for properties. #400

Closed
bas080 opened this issue Jan 9, 2017 · 3 comments
Closed

Incrementally add rules for properties. #400

bas080 opened this issue Jan 9, 2017 · 3 comments

Comments

@bas080
Copy link

bas080 commented Jan 9, 2017

I noticed that the current implementation 1.0.0-beta.1.0.1 does not allow me "merge" multiple rules/options for a property's validation.

ValidationRules
  .ensure('name')
  .displayName('product name')
  .ensure('name')
  .required()
  .on(Product);

It ignores my previously defined displayName.

So why would I want to do this? I have this function which takes an aurelia-orm entity and creates some default validations for it based on the entity's orm metadata. Some basic validation for free is great!

export function entityValidationRules(entity) {
  const metadata     = OrmMetadata.forTarget(entity);
  const associations = metadata.fetch('associations');
  const types        = metadata.fetch('types');
  const enumerations = metadata.fetch('enumerations');
  const data         = metadata.fetch('data');
  const metadataKeys = [associations, types, enumerations].reduce(
    (keys, obj) => keys.concat(obj ? Object.keys(obj) : []),
    []
  );

  return metadataKeys.reduce((ensure, key) => {
    ensure = ValidationRules.ensure(key);

    if (data && data[key] && data[key].form && data[key].form.label) {
      console.log(data[key].form)
      ensure = ensure.displayName(data[key].form.label);
    }

    if (types && types[key]) {
      ensure = ensure
        .satisfies(satisfiesType(types[key]))
        .withMessage(`${key} should be an ${types[key]}`);
    }

    if (enumerations && enumerations[key]) {
      ensure = ensure
      .satisfies(satisfiesEnumeration(enumerations[key]))
      .withMessage(`${key} should be one of the following values ${humanEnumeration(enumerations[key])}`);
    }

    if (associations && associations[key]) {
      ensure = ensure
      .satisfies(satisfiesAssocation(associations[key]));
    }

    return ensure;
  }, ValidationRules);
}

I then return the last ensure return value and continue adding more rules.

  entityValidationRules(Language)
    .ensure('name')
    .minLength(2)
    .required()

    .ensure('locale')
    .required()
    .satisfiesRule('isLocale')

    .ensure('language')
    .required()
    .satisfiesRule('isLanguage')
    .on(Language);

And now it ignores the validation options I defined in the entityValidationRules. Is that desired behavior?

I could fix it by implementing entityValidationRules differently.

entityValidationRules({
  name: ensure => ensure.required().satisfiesRule('isLanguage')
  // ... //
}, Language);

But I prefer using the API as it is documented.

@jdanyow
Copy link
Contributor

jdanyow commented Jan 15, 2017

This sounds like something we ought to support.

@dkent600 do you think this would also solve #363 ?

@dkent600
Copy link
Contributor

@jdanyow Sure looks like it would help with #363.

@dkent600
Copy link
Contributor

Just to be thorough, the original use case is not supported without #363:

ValidationRules
  .ensure('name')
  .displayName('product name')
  .ensure('name')
  .required()
  .on(Product);

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

No branches or pull requests

4 participants