#lang scheme/base
(require "util.scm"
"record.scm")
(provide validate field-validate)
(define (validate . validation-fns)
(lambda (rec)
(let ((errors (filter-map (lambda (f) (f rec)) validation-fns)))
(if (empty? errors)
#f
(string-join errors "\n")))))
(define (field-validate field-name (pred #f))
(lambda (rec)
(aif (rec-prop rec field-name)
(if pred
(if (pred it)
#f
(format "Validation error triggered by failure of ~A on field '~A'."
pred field-name))
#f)
(format "Missing field '~A'." field-name))))