File tree 4 files changed +60
-5
lines changed
4 files changed +60
-5
lines changed Original file line number Diff line number Diff line change 1709
1709
(let [validator (-memoize (fn [] (-validator (rf ))))]
1710
1710
(fn [x] ((validator ) x))))
1711
1711
(-explainer [_ path]
1712
- (let [explainer (-memoize (fn [] (-explainer (rf ) (conj path 0 ))))]
1712
+ (let [explainer (-memoize (fn [] (-explainer (rf ) (into path [ 0 0 ] ))))]
1713
1713
(fn [x in acc] ((explainer ) x in acc))))
1714
1714
(-parser [_] (->parser -parser))
1715
1715
(-unparser [_] (->parser -unparser))
Original file line number Diff line number Diff line change 624
624
625
625
(testing " ref schemas"
626
626
627
+ (let [schema [:ref {:registry {::referred [:map [:foo :int ]]}} ::referred ]]
628
+ (is (nil? (m/explain schema {:foo 2 })))
629
+ (testing " explain path"
630
+ (let [exp (m/explain schema {:foo " 2" })]
631
+ (is (results= {:value {:foo " 2" }
632
+ :schema schema
633
+ :errors [{:in [:foo ]
634
+ :path [0 0 :foo ]
635
+ :schema :int
636
+ :value " 2" }]}
637
+ exp))
638
+ (is (form= :int (mu/get-in schema (-> exp :errors first :path )))))))
639
+
627
640
(testing " invalid refs fail"
628
641
(is (thrown?
629
642
#?(:clj Exception, :cljs js/Error)
640
653
(is (results= {:schema ConsCell
641
654
:value [1 [2 ]]
642
655
:errors [{:in [1 ]
643
- :path [0 0 0 1 0 0 ]
656
+ :path [0 0 0 1 0 0 0 ]
644
657
:schema (mu/get-in ConsCell [0 0 0 ])
645
658
:type :malli.core/tuple-size
646
659
:value [2 ]}]}
3333
3346
3334
3347
(testing " it works"
3335
3348
(is (= User (m/form schema)))
3336
- (is (every? (m/validator schema) (mg/sample schema {:seed 100 }))))))
3349
+ (is (every? (m/validator schema) (mg/sample schema {:seed 100 }))))
3350
+
3351
+ (testing " explain path"
3352
+ (let [exp (m/explain schema {:id 1 })]
3353
+ (is (results= {:value {:id 1 }
3354
+ :schema User
3355
+ :errors [{:in [:id ]
3356
+ :path [:id 0 ]
3357
+ :schema :string
3358
+ :value 1 }]}
3359
+ exp))
3360
+ (is (form= :string (mu/get-in schema (-> exp :errors first :path )))))
3361
+ (let [explicit-ref [:ref #'UserId]
3362
+ exp (m/explain explicit-ref 1 )]
3363
+ (is (results= {:value 1
3364
+ :schema explicit-ref
3365
+ :errors [{:in []
3366
+ :path [0 0 ]
3367
+ :schema :string
3368
+ :value 1 }]}
3369
+ exp))
3370
+ (is (form= :string (mu/get-in explicit-ref (-> exp :errors first :path ))))))))
3337
3371
3338
3372
#?(:clj
3339
3373
(deftest roundrobin-var-references
Original file line number Diff line number Diff line change 545
545
(m/explain [1 2 :foo ])
546
546
(me/humanize )))))
547
547
548
+ (def VarSchema [:map [:foo :int ]])
549
+
548
550
(deftest error-definion-lookup-test
549
551
(is (= {:foo [" should be an integer" ]}
550
552
(-> [:map
629
631
(= password password2))]]
630
632
(m/explain {:password " secret"
631
633
:password2 " faarao" })
632
- (me/humanize {:resolve me/-resolve-root-error}))))))
634
+ (me/humanize {:resolve me/-resolve-root-error})))))
635
+
636
+ (testing " refs #1106"
637
+ (is (= {:foo [" should be an integer" ]}
638
+ (me/humanize
639
+ (m/explain [:ref #'VarSchema] {:foo " 2" })
640
+ {:resolve me/-resolve-direct-error})))
641
+ (is (= {:foo [" should be an integer" ]}
642
+ (me/humanize
643
+ (m/explain [:ref #'VarSchema] {:foo " 2" })
644
+ {:resolve me/-resolve-root-error})))))
633
645
634
646
(deftest limits
635
647
(is (= {:a [[" should be an int" ]]
Original file line number Diff line number Diff line change 276
276
; ; LensSchemas
277
277
; ;
278
278
279
+ (def Var :string )
280
+
279
281
(deftest basic-lens-schema-test
280
282
(let [re #"kikka"
281
283
int? (m/schema int?)]
333
335
334
336
[:ref {:registry {::a int?, ::b string?}} ::a ] 0 ::a
335
337
[:ref {:registry {::a int?, ::b string?}} ::a ] 1 nil
338
+ [:ref #'Var] 0 #'Var
339
+ [:ref #'Var] 1 nil
336
340
337
341
[:schema int?] 0 int?
338
342
[:schema int?] 1 nil )
439
443
(is (form= (mu/get-in (m/schema [:ref {:registry {::a int?, ::b string?}} ::a ]) [0 ]) ::a ))
440
444
(is (mu/equals (mu/get-in (m/schema [:ref {:registry {::a int?, ::b string?}} ::a ]) [0 0 ]) int?))
441
445
(is (form= (mu/get-in (m/schema [:schema {:registry {::a int?, ::b string?}} ::a ]) [0 ]) ::a ))
442
- (is (mu/equals (mu/get-in (m/schema [:schema {:registry {::a int?, ::b string?}} ::a ]) [0 0 ]) int?)))
446
+ (is (mu/equals (mu/get-in (m/schema [:schema {:registry {::a int?, ::b string?}} ::a ]) [0 0 ]) int?))
447
+
448
+ (is (form= (mu/get-in (m/schema [:ref #'Var]) [0 ]) #'Var))
449
+ (is (form= (mu/get-in (m/schema [:ref #'Var]) [0 0 ]) :string ))
450
+ (is (form= (mu/get-in (m/schema [:schema #'Var]) [0 ]) #'Var))
451
+ (is (form= (mu/get-in (m/schema [:schema #'Var]) [0 0 ]) :string )))
443
452
444
453
(deftest dissoc-test
445
454
(let [schema [:map {:title " map" }
You can’t perform that action at this time.
0 commit comments