jelhan/croodle

View on GitHub
app/components/bs-form.js

Summary

Maintainability
A
0 mins
Test Coverage
import BaseBsForm from 'ember-bootstrap/components/bs-form';
import IntlMessage from '../utils/intl-message';

export default class BsForm extends BaseBsForm {
  '__ember-bootstrap_subclass' = true;

  get hasValidator() {
    return true;
  }

  async validate(model) {
    const isInvalid = Object.getOwnPropertyNames(
      Object.getPrototypeOf(model),
    ).some((potentialValidationKey) => {
      // Validation getters must be named `propertyValidation` by our convention
      if (!potentialValidationKey.endsWith('Validation')) {
        return false;
      }

      // Validation errors must be an instance of IntlMessage by convention
      return model[potentialValidationKey] instanceof IntlMessage;
    });

    if (isInvalid) {
      throw new Error();
    }
  }
}