/src/components/ErrorValidation/index.js

https://github.com/flyve-mdm/web-mdm-dashboard · JavaScript · 128 lines · 85 code · 3 blank · 40 comment · 11 complexity · 235ef524d78036148424732cca3e0ca4 MD5 · raw file

  1. /*
  2. * Copyright © 2018 Teclib. All rights reserved.
  3. *
  4. * This file is part of web-mdm-dashboard
  5. *
  6. * web-mdm-dashboard is a subproject of Flyve MDM. Flyve MDM is a mobile
  7. * device management software.
  8. *
  9. * Flyve MDM is free software: you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 3
  12. * of the License, or (at your option) any later version.
  13. *
  14. * Flyve MDM is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. * ------------------------------------------------------------------------------
  19. * @author Gianfranco Manganiello (gmanganiello@teclib.com)
  20. * @author Hector Rondon (hrondon@teclib.com)
  21. * @copyright Copyright © 2018 Teclib. All rights reserved.
  22. * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
  23. * @link https://github.com/flyve-mdm/web-mdm-dashboard
  24. * @link http://flyve.org/web-mdm-dashboard
  25. * @link https://flyve-mdm.com
  26. * ------------------------------------------------------------------------------
  27. */
  28. import React, {
  29. PureComponent,
  30. } from 'react'
  31. import PropTypes from 'prop-types'
  32. import I18n from 'shared/i18n'
  33. /**
  34. * Component to errors of a form field
  35. * @class ErrorValidation
  36. * @extends PureComponent
  37. */
  38. class ErrorValidation extends PureComponent {
  39. /**
  40. * Validate a data according to determined parameters
  41. * @static
  42. * @function validation
  43. * @param {object} parametersToEvaluate
  44. * @param {*} value
  45. * @return {object} shows if there are errors or not, and what are these
  46. */
  47. static validation = (parametersToEvaluate, value) => {
  48. const errorMessages = []
  49. if (parametersToEvaluate.isRequired && !value) { errorMessages.push(I18n.t('validation.required_field')) }
  50. if (parametersToEvaluate.minimunLength) {
  51. if (value.length < parametersToEvaluate.minimunLength) {
  52. errorMessages.push(
  53. `${I18n.t('validation.insufficient_characters')} ${parametersToEvaluate.minimunLength} ${I18n.t('validation.is_requiered')}`,
  54. )
  55. }
  56. }
  57. if (parametersToEvaluate.needDigit) {
  58. const myRe = /[\d]/g
  59. if (!myRe.test(value)) { errorMessages.push(I18n.t('validation.one_digit')) }
  60. }
  61. if (parametersToEvaluate.needLowercaseCharacter) {
  62. const myRe = /[a-z]/g
  63. if (!myRe.test(value)) { errorMessages.push(I18n.t('validation.lowercase_character')) }
  64. }
  65. if (parametersToEvaluate.needUppercaseCharacter) {
  66. const myRe = /[A-Z]/g
  67. if (!myRe.test(value)) { errorMessages.push(I18n.t('validation.uppercase_character')) }
  68. }
  69. if (parametersToEvaluate.needSymbol) {
  70. const myRe = /[!@#%^&*?><)(+=._\-\\[\]^~`'"˜$ˆ/:;{}|]/g
  71. if (!myRe.test(value)) { errorMessages.push(I18n.t('validation.special_character')) }
  72. }
  73. if (parametersToEvaluate.isEmail) {
  74. const myRe = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/g
  75. if (!myRe.test(value)) { errorMessages.push(I18n.t('validation.invalid_email')) }
  76. }
  77. if (parametersToEvaluate.isEqualTo) {
  78. if (value !== parametersToEvaluate.isEqualTo.value) { errorMessages.push(parametersToEvaluate.isEqualTo.message) }
  79. }
  80. if (parametersToEvaluate.customValidation) {
  81. if (parametersToEvaluate.customValidation(value)) { errorMessages.push(parametersToEvaluate.customValidation(value)) }
  82. }
  83. const result = (errorMessages.length <= 0) ? {
  84. isCorrect: true,
  85. errors: [],
  86. } : {
  87. isCorrect: false,
  88. errors: errorMessages,
  89. }
  90. return result
  91. }
  92. /**
  93. * Render component
  94. * @function render
  95. */
  96. render() {
  97. return (
  98. <div className="error-message">
  99. <ul>
  100. {
  101. this.props.errors.map((element, index) => (
  102. <li key={`errorMessage-${index.toString()}`}>
  103. -
  104. {element}
  105. </li>
  106. ))
  107. }
  108. </ul>
  109. </div>
  110. )
  111. }
  112. }
  113. ErrorValidation.defaultProps = {
  114. errors: [],
  115. }
  116. ErrorValidation.propTypes = {
  117. errors: PropTypes.array,
  118. }
  119. export default ErrorValidation