001  (ns dog-and-duck.quack.picky.collections
002    (:require [dog-and-duck.quack.picky.utils :refer [concat-non-empty
003                                                      cond-make-fault-object
004                                                      object-faults
005                                                      object-reference-or-faults]]))
006  
007  
008  ;;;     Copyright (C) Simon Brooke, 2022
009  
010  ;;;     This program is free software; you can redistribute it and/or
011  ;;;     modify it under the terms of the GNU General Public License
012  ;;;     as published by the Free Software Foundation; either version 2
013  ;;;     of the License, or (at your option) any later version.
014  
015  ;;;     This program is distributed in the hope that it will be useful,
016  ;;;     but WITHOUT ANY WARRANTY; without even the implied warranty of
017  ;;;     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
018  ;;;     GNU General Public License for more details.
019  
020  ;;;     You should have received a copy of the GNU General Public License
021  ;;;     along with this program; if not, write to the Free Software
022  ;;;     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
023  
024  (defn paged-collection-faults
025    "Return a list of faults found in `x` considered as a paged collection
026     object of this sub-`type`, or `nil` if none are found."
027    [x type]
028    (concat-non-empty
029     (object-faults x type)
030     (list (object-reference-or-faults x type :critical :expected-collection)
031           (cond-make-fault-object (integer? (:totalItems x)) :should :no-total-items)
032           (object-reference-or-faults (:first x) nil :must :no-first-page)
033           (object-reference-or-faults (:last x) nil :should :no-last-page))))
034  
035  (defn simple-collection-faults
036    "Return a list of faults found in `x` considered as a non-paged collection
037     object of this sub-`type`, or `nil` if none are found."
038    [x type]
039    (concat-non-empty
040     (object-faults x type)
041     (cons
042      (list (object-reference-or-faults x type :critical :expected-collection)
043            (cond-make-fault-object (integer? (:totalItems x)) :should :no-total-items)
044            (cond-make-fault-object (coll? (:items x)) :must :no-items-collection))
045      (map #(object-reference-or-faults % nil :must :not-object-reference) (:items x)))))
046  
047  (defn collection-page-faults
048    [x type]
049    (concat-non-empty
050     (simple-collection-faults x type)
051     (list
052      (object-reference-or-faults (:partOf x)
053                                  (apply str (drop-last 4 type))
054                                  :should
055                                  :n-part-of)
056      (object-reference-or-faults (:next x) type :minor :no-next-page)
057      (object-reference-or-faults (:prev x) type :minor :no-prev-page))))