FormErrors.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. export default class Errors {
  2. /**
  3. * Create a new error bag instance.
  4. */
  5. constructor () {
  6. this.errors = {}
  7. }
  8. /**
  9. * Set the errors object or field error messages.
  10. *
  11. * @param {Object|String} field
  12. * @param {Array|String|undefined} messages
  13. */
  14. set (field, messages) {
  15. if (typeof field === 'object') {
  16. this.errors = field
  17. } else {
  18. this.set({ ...this.errors, [field]: arrayWrap(messages) })
  19. }
  20. }
  21. /**
  22. * Get all the errors.
  23. *
  24. * @return {Object}
  25. */
  26. all () {
  27. return this.errors
  28. }
  29. /**
  30. * Determine if there is an error for the given field.
  31. *
  32. * @param {String} field
  33. * @return {Boolean}
  34. */
  35. has (field) {
  36. return this.errors.hasOwnProperty(field)
  37. }
  38. /**
  39. * Determine if there are any errors for the given fields.
  40. *
  41. * @param {...String} fields
  42. * @return {Boolean}
  43. */
  44. hasAny (...fields) {
  45. return fields.some(field => this.has(field))
  46. }
  47. /**
  48. * Determine if there are any errors.
  49. *
  50. * @return {Boolean}
  51. */
  52. any () {
  53. return Object.keys(this.errors).length > 0
  54. }
  55. /**
  56. * Get the first error message for the given field.
  57. *
  58. * @param String} field
  59. * @return {String|undefined}
  60. */
  61. get (field) {
  62. if (this.has(field)) {
  63. return this.getAll(field)[0]
  64. }
  65. }
  66. /**
  67. * Get all the error messages for the given field.
  68. *
  69. * @param {String} field
  70. * @return {Array}
  71. */
  72. getAll (field) {
  73. return arrayWrap(this.errors[field] || [])
  74. }
  75. /**
  76. * Get the error message for the given fields.
  77. *
  78. * @param {...String} fields
  79. * @return {Array}
  80. */
  81. only (...fields) {
  82. const messages = []
  83. fields.forEach(field => {
  84. const message = this.get(field)
  85. if (message) {
  86. messages.push(message)
  87. }
  88. })
  89. return messages
  90. }
  91. /**
  92. * Get all the errors in a flat array.
  93. *
  94. * @return {Array}
  95. */
  96. flatten () {
  97. return Object.values(this.errors).reduce((a, b) => a.concat(b), [])
  98. }
  99. /**
  100. * Clear one or all error fields.
  101. *
  102. * @param {String|undefined} field
  103. */
  104. clear (field) {
  105. const errors = {}
  106. if (field) {
  107. Object.keys(this.errors).forEach(key => {
  108. if (key !== field) {
  109. errors[key] = this.errors[key]
  110. }
  111. })
  112. }
  113. this.set(errors)
  114. }
  115. }
  116. /**
  117. * If the given value is not an array, wrap it in one.
  118. *
  119. * @param {Any} value
  120. * @return {Array}
  121. */
  122. function arrayWrap (value) {
  123. return Array.isArray(value) ? value : [value]
  124. }