cowbell/splittypie

View on GitHub
app/validators/array.js

Summary

Maintainability
A
0 mins
Test Coverage
import { isArray } from "@ember/array";
import { get } from "@ember/object";
import Base from "ember-validations/validators/base";

export default Base.extend({
    init() {
        // this call is necessary, don't forget it!
        this._super();

        this.dependentValidationKeys.pushObject(`${this.property}.@each.isValid`);
    },

    call() {
        const arrayToValidate = get(this.model, this.property);

        if (!arrayToValidate) {
            return;
        }

        if (!isArray(arrayToValidate)) {
            throw new Error(
                `property ${this.property} is not an array, but array validator is used`
            );
        }

        if (arrayToValidate.isAny("isValid", false)) {
            this.errors.pushObject("not every element valid");
        }
    },
});