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

mendersoftware / gui / 1057188406

01 Nov 2023 04:24AM UTC coverage: 82.824% (-17.1%) from 99.964%
1057188406

Pull #4134

gitlab-ci

web-flow
chore: Bump uuid from 9.0.0 to 9.0.1

Bumps [uuid](https://github.com/uuidjs/uuid) from 9.0.0 to 9.0.1.
- [Changelog](https://github.com/uuidjs/uuid/blob/main/CHANGELOG.md)
- [Commits](https://github.com/uuidjs/uuid/compare/v9.0.0...v9.0.1)

---
updated-dependencies:
- dependency-name: uuid
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #4134: chore: Bump uuid from 9.0.0 to 9.0.1

4349 of 6284 branches covered (0.0%)

8313 of 10037 relevant lines covered (82.82%)

200.97 hits per line

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

83.67
/src/js/components/common/forms/form.js
1
// Copyright 2016 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React from 'react';
15
import { FormProvider, useForm } from 'react-hook-form';
16

17
import { Button } from '@mui/material';
18

19
import validator from 'validator';
20

21
const getErrorMsg = (validateMethod, args) => {
18✔
22
  switch (validateMethod) {
264!
23
    case 'isLength':
24
      if (args[0] === 1) {
51!
25
        return 'This field is required';
×
26
      } else if (args[0] > 1) {
51!
27
        return `Must be at least ${args[0]} characters long`;
51✔
28
      }
29
      break;
×
30
    case 'isAlpha':
31
      return 'This field must contain only letters';
×
32
    case 'isAlphanumeric':
33
      return 'This field must contain only letters or numbers';
×
34
    case 'isNumeric':
35
      return 'Please enter a valid code';
×
36
    case 'isEmail':
37
      return 'Please enter a valid email address';
181✔
38
    case 'isNot':
39
      if (args[0] === args[1]) {
32!
40
        return `This field should have a value other than ${args[0]}`;
×
41
      }
42
      break;
32✔
43
    default:
44
      return 'There is an error with this field';
×
45
  }
46
};
47

48
const tryApplyValidations = (value, validations, initialValidationResult) =>
18✔
49
  validations.split(',').reduce((accu, validation) => {
655✔
50
    if (!accu.isValid || !validation) {
1,046✔
51
      return accu;
172✔
52
    }
53
    var args = validation.split(':');
874✔
54
    var validateMethod = args.shift();
874✔
55
    // We use JSON.parse to convert the string values passed to the
56
    // correct type. Ex. 'isLength:1' will make '1' actually a number
57
    args = args.map(arg => JSON.parse(JSON.stringify(arg)));
874✔
58

59
    var tmpArgs = args;
874✔
60
    // We then merge two arrays, ending up with the value
61
    // to pass first, then options, if any. ['valueFromInput', 5]
62
    args = [value].concat(args);
874✔
63
    try {
874✔
64
      // So the next line of code is actually:
65
      // validator.isLength('valueFromInput', 5)
66
      if (!validator[validateMethod].apply(validator, args)) {
874✔
67
        return { errortext: getErrorMsg(validateMethod, tmpArgs), isValid: false };
232✔
68
      }
69
    } catch {
70
      const errortext = getErrorMsg(validateMethod, args) || '';
32✔
71
      return { errortext, isValid: !errortext };
32✔
72
    }
73
    return accu;
610✔
74
  }, initialValidationResult);
75

76
const runPasswordValidations = ({ required, value, validations, isValid, errortext }) => {
18✔
77
  if (required && !value) {
310!
78
    return { isValid: false, errortext: 'Password is required' };
×
79
  } else if (required || value) {
310✔
80
    isValid = tryApplyValidations(value, validations, { isValid, errortext }).isValid;
300✔
81
    return { isValid, errortext: !isValid ? 'Password too weak' : errortext };
300✔
82
  }
83
  return { isValid, errortext };
10✔
84
};
85

86
export const runValidations = ({ required, value, id, validations }) => {
18✔
87
  let isValid = true;
737✔
88
  let errortext = '';
737✔
89
  if (id && id.includes('password')) {
737✔
90
    return runPasswordValidations({ required, value, validations, isValid, errortext });
310✔
91
  } else {
92
    if (value || required) {
427✔
93
      return tryApplyValidations(value, validations, { isValid, errortext });
355✔
94
    }
95
  }
96
  return { isValid, errortext };
72✔
97
};
98

99
export const Form = ({
18✔
100
  autocomplete,
101
  buttonColor,
102
  children,
103
  className = '',
380✔
104
  defaultValues = {},
738✔
105
  handleCancel,
106
  id,
107
  onSubmit,
108
  showButtons,
109
  submitButtonId,
110
  submitLabel
111
}) => {
112
  const methods = useForm({ mode: 'onChange', defaultValues });
853✔
113
  const {
114
    handleSubmit,
115
    formState: { isValid }
116
  } = methods;
853✔
117
  return (
853✔
118
    <FormProvider {...methods}>
119
      <form autoComplete={autocomplete} className={className} id={id} noValidate onSubmit={handleSubmit(onSubmit)}>
120
        {children}
121
        {!!showButtons && (
1,625✔
122
          <div className="flexbox" style={{ justifyContent: 'flex-end', height: 'min-content', marginTop: 32 }}>
123
            {!!handleCancel && (
835✔
124
              <Button key="cancel" onClick={handleCancel} style={{ marginRight: 10, display: 'inline-block' }}>
125
                Cancel
126
              </Button>
127
            )}
128
            <Button variant="contained" type="submit" disabled={!isValid} id={submitButtonId} color={buttonColor}>
129
              {submitLabel}
130
            </Button>
131
          </div>
132
        )}
133
      </form>
134
    </FormProvider>
135
  );
136
};
137

138
export default Form;
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

© 2025 Coveralls, Inc