• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CenterForOpenScience / ember-osf-web / 5543708847

pending completion
5543708847

push

github

web-flow
Merge pull request #1904 from CenterForOpenScience/basket/cesium

Basket/cesium

2490 of 3649 branches covered (68.24%)

Branch coverage included in aggregate %.

101 of 101 new or added lines in 25 files covered. (100.0%)

5711 of 7667 relevant lines covered (74.49%)

244.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.49
/lib/osf-components/addon/components/validated-input/base-component.ts
1
// This component is derived from ember-cp-validations.
2
// See https://github.com/offirgolan/ember-cp-validations for more information
3
import Model from '@ember-data/model';
4
import Component from '@ember/component';
5
import { computed, defineProperty } from '@ember/object';
6
import { dependentKeyCompat } from '@ember/object/compat';
7
import { alias, oneWay } from '@ember/object/computed';
8
import { inject as service } from '@ember/service';
9
import { isEmpty } from '@ember/utils';
10
import { BufferedChangeset } from 'ember-changeset/types';
11

12
import { ResultCollection } from 'ember-cp-validations';
13
import { AttributesFor, RelationshipsFor } from 'ember-data';
14
import Intl from 'ember-intl/services/intl';
15

16
export enum ValidationStatus {
17
    Hidden,
18
    Success,
19
    HasError,
20
    HasWarning,
21
}
22

23
export default abstract class BaseValidatedInput<M extends Model> extends Component {
24
    // Required arguments
25
    valuePath!: AttributesFor<M> | RelationshipsFor<M>;
26

27
    // Optional arguments
28
    changeset?: BufferedChangeset & M;
29
    label?: string;
30
    ariaLabel?: string;
31
    placeholder?: string;
32
    disabled = false;
389✔
33
    shouldShowMessages = true;
389✔
34
    model?: M;
35
    isRequired?: Boolean;
36

37
    // Private properties
38
    @service intl!: Intl;
39

40
    // defined in constructor
41
    errors?: string[];
42
    value: any;
43
    isInvalid?: boolean;
44
    isValidating?: boolean;
45
    validation?: ResultCollection;
46

47
    @computed('errors', 'validation.options', 'isRequired')
48
    get required(): boolean {
49
        if (!this.validation) {
94✔
50
            return false;
47✔
51
        }
52
        if (this.isRequired === true) {
47!
53
            return true;
×
54
        }
55
        if (this.isRequired === false) {
47!
56
            return false;
×
57
        }
58
        const { options } = this.validation;
47✔
59
        if (!options) {
47!
60
            return false;
×
61
        }
62
        if (!options.presence) {
47!
63
            return false;
×
64
        }
65
        if (options.presence.disabled) {
47✔
66
            return false;
6✔
67
        }
68
        if (options.presence.presence) {
41!
69
            return true;
41✔
70
        }
71
        return false;
×
72
    }
73

74
    @computed('placeholder', 'required')
75
    get _placeholder(): string {
76
        return this.placeholder || this.intl.t(this.required ? 'general.required' : 'general.optional');
334✔
77
    }
78

79
    @dependentKeyCompat
80
    get _isInvalid() {
81
        return this.changeset ? Boolean(this.changeset.error[this.valuePath as string]) : this.isInvalid;
963✔
82
    }
83

84
    @computed('shouldShowMessages', 'value', '_isInvalid', 'isInvalid', 'isValidating')
85
    get validationStatus(): ValidationStatus {
86
        const {
87
            shouldShowMessages,
88
            value,
89
            validation,
90
            _isValidating,
91
            _isInvalid,
92
        } = this;
963✔
93
        switch (true) {
963✔
94
        case !shouldShowMessages || _isValidating:
1,539✔
95
            return ValidationStatus.Hidden;
411✔
96
        case _isInvalid:
97
            return ValidationStatus.HasError;
62✔
98
        case validation && !isEmpty(validation.warnings):
623✔
99
            return ValidationStatus.HasWarning;
1✔
100
        case isEmpty(value):
101
            return ValidationStatus.Hidden;
163✔
102
        default:
103
            return ValidationStatus.Success;
326✔
104
        }
105
    }
106

107
    get _isValidating() {
108
        return this.changeset ? this.changeset.isValidating(this.valuePath as string) : this.isValidating;
963✔
109
    }
110

111
    init() {
112
        super.init();
389✔
113
        if (this.changeset) {
389✔
114
            defineProperty(this, 'validation', oneWay(`changeset.data.validations.attrs.${this.valuePath}`));
208✔
115
            defineProperty(this, 'errors', oneWay(`changeset.error.${this.valuePath}.validation`));
208✔
116
            defineProperty(this, 'value', alias(`changeset.${this.valuePath}`));
208✔
117
        } else if (this.model) {
181✔
118
            defineProperty(this, 'validation', oneWay(`model.validations.attrs.${this.valuePath}`));
179✔
119
            defineProperty(this, 'errors', oneWay(`model.validations.attrs.${this.valuePath}.errors`));
179✔
120
            defineProperty(this, 'value', alias(`model.${this.valuePath}`));
179✔
121
            defineProperty(this, 'isValidating', oneWay(`model.validations.attrs.${this.valuePath}.isValidating`));
179✔
122
            defineProperty(this, 'isInvalid', oneWay(`model.validations.attrs.${this.valuePath}.isInvalid`));
179✔
123
        }
124
    }
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc