Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

184 рядки
4.2 KiB

  1. /* eslint-env mocha */
  2. import expect from "expect"
  3. import { transformPathToArray } from "core/path-translator"
  4. describe("validation plugin - path translator", function(){
  5. describe("string paths", function(){
  6. it("should translate a simple string path to an array", function(){
  7. // Given
  8. let jsSpec = {
  9. one: {
  10. a: "a thing",
  11. b: "another thing",
  12. c: "one more thing"
  13. },
  14. two: 2
  15. }
  16. let path = "instance.one.a"
  17. // Then
  18. expect(transformPathToArray(path, jsSpec)).toEqual(["one", "a"])
  19. })
  20. it("should translate an ambiguous string path to an array", function(){
  21. // Since JSONSchema uses periods to mark different properties,
  22. // a key with a period in it is ambiguous, because it can mean at least two things.
  23. // In our case, the path can mean:
  24. // ["google", "com", "a"] or ["google.com", "a"]
  25. // Given
  26. let jsSpec = {
  27. "google.com": {
  28. a: "a thing",
  29. b: "another thing",
  30. c: "one more thing"
  31. },
  32. "gmail.com": {
  33. d: "more stuff",
  34. e: "even more stuff"
  35. }
  36. }
  37. let path = "instance.google.com.a"
  38. // Then
  39. expect(transformPathToArray(path, jsSpec)).toEqual(["google.com", "a"])
  40. })
  41. it("should translate an doubly ambiguous string path to an array", function(){
  42. // Since JSONSchema uses periods to mark different properties,
  43. // a key with two periods in it (like "www.google.com") is doubly ambiguous,
  44. // because it can mean at least three things.
  45. // Given
  46. let jsSpec = {
  47. "www.google.com": {
  48. a: "a thing",
  49. b: "another thing",
  50. c: "one more thing"
  51. },
  52. "gmail.com": {
  53. d: "more stuff",
  54. e: "even more stuff"
  55. }
  56. }
  57. let path = "instance.www.google.com.a"
  58. // Then
  59. expect(transformPathToArray(path, jsSpec)).toEqual(["www.google.com", "a"])
  60. })
  61. it("should return null for an invalid path", function(){
  62. // Given
  63. let jsSpec = {
  64. "google.com": {
  65. a: "a thing",
  66. b: "another thing",
  67. c: "one more thing"
  68. },
  69. "gmail.com": {
  70. d: "more stuff",
  71. e: "even more stuff"
  72. }
  73. }
  74. let path = "instance.google.net.a"
  75. // Then
  76. expect(transformPathToArray(path, jsSpec)).toEqual(null)
  77. })
  78. it("should return inline array indices in their own value", function(){
  79. // "a[1]" => ["a", "1"]
  80. // Given
  81. let jsSpec = {
  82. "google.com": {
  83. a: [
  84. "hello",
  85. "here is the target"
  86. ],
  87. b: "another thing",
  88. c: "one more thing"
  89. },
  90. "gmail.com": {
  91. d: "more stuff",
  92. e: "even more stuff"
  93. }
  94. }
  95. let path = "instance.google.com.a[1]"
  96. // Then
  97. expect(transformPathToArray(path, jsSpec)).toEqual(["google.com", "a", "1"])
  98. })
  99. it("should return the correct path when the last part is ambiguous", function(){
  100. // Given
  101. let jsSpec = {
  102. "google.com": {
  103. a: [
  104. "hello",
  105. {
  106. "gmail.com": 1234
  107. }
  108. ],
  109. b: "another thing",
  110. c: "one more thing"
  111. },
  112. "gmail.com": {
  113. d: "more stuff",
  114. e: "even more stuff"
  115. }
  116. }
  117. let path = "instance.google.com.a[1].gmail.com"
  118. // Then
  119. expect(transformPathToArray(path, jsSpec)).toEqual(["google.com", "a", "1", "gmail.com"])
  120. })
  121. it("should return the correct path when the last part is doubly ambiguous", function(){
  122. // Given
  123. let jsSpec = {
  124. "google.com": {
  125. a: [
  126. "hello",
  127. {
  128. "www.gmail.com": 1234
  129. }
  130. ],
  131. b: "another thing",
  132. c: "one more thing"
  133. },
  134. "gmail.com": {
  135. d: "more stuff",
  136. e: "even more stuff"
  137. }
  138. }
  139. let path = "instance.google.com.a[1].www.gmail.com"
  140. // Then
  141. expect(transformPathToArray(path, jsSpec)).toEqual(["google.com", "a", "1", "www.gmail.com"])
  142. })
  143. })
  144. })