1717
1818(def SCHEMA_CACHE_TIME
1919 " The number of milliseconds schema validation functions will be cached."
20- (* 60 60 1000 )) ; ; 1 hour
20+ (* 24 60 60 1000 )) ; ; 24 hours
2121
2222(defn create-schema-validation-cache
2323 " Creates a cache for schema validation functions."
4343 get-field-values (fn [c fs]
4444 (let [metadata (json/parse-string (:metadata c) true )]
4545 (mapv #(let [field-path (rest (string/split % #"\. " ))
46- keyword-path (keyword ( first field-path) )]
47- (get-in metadata[ keyword-path] ))
46+ keyword-path (mapv keyword field-path)]
47+ (get-in metadata keyword-path))
4848 fs)))
4949
5050 ; ; Extract values for the specified fields from the current concept
5151 field-values (get-field-values concept fields)
5252
5353 ; ; Check if any other document in the collection has the same combination of values
5454 duplicate-concepts (filter (fn [existing-concept]
55- ; ; Debug print to show values being compared
5655 (and (not= (:native-id existing-concept) (:native-id concept))
5756 (not= (:deleted existing-concept) true )
5857 (= (set (get-field-values existing-concept fields))
7271 [])))
7372
7473(defn- validate-by-type
75- " Validates fields based on validation type.
74+ " Validates fields based on validation type. Any new validation added to the config.json
75+ should be added here and implemented in the corresponding function.
76+ validation-value is here for possible future implementations but is not needed in the
77+ uniqueness validation.
7678 Returns a sequence of error messages if validation fails, empty sequence otherwise."
7779 [context concept validation-type fields validation-value]
7880 (case validation-type
98100 " Loads a single schema validation function for a concept type and version"
99101 [concept-type version]
100102 (try
101- (if (generics/approved-generic? concept-type version)
102- (let [schema-json (generics/read-schema-config concept-type version)
103- schema (json/parse-string schema-json true )]
104- (fn [context concept]
105- (validate-with-schema context concept schema)))
106- (do
107- (error " Schema version not approved for" concept-type " version" version)
108- nil ))
103+ (let [schema-json (generics/read-schema-config concept-type version)
104+ schema (json/parse-string schema-json true )]
105+ (fn [context concept]
106+ (validate-with-schema context concept schema)))
109107 (catch Exception e
110108 (error " Error loading schema for" concept-type " version" version " :" (.getMessage e))
111109 nil )))
123121 []
124122 (info " Loading schema validation functions for all generic concept types" )
125123 (let [generic-types (concepts/get-generic-concept-types-array )
126- validators (reduce (fn [validators concept-type]
124+ validators (reduce (fn [vs concept-type]
127125 (let [current-version (generics/current-generic-version concept-type)
128126 validator (load-schema-validation concept-type current-version)]
129127 (if validator
130- (assoc validators [concept-type current-version] validator)
131- validators )))
128+ (assoc vs [concept-type current-version] validator)
129+ vs )))
132130 {}
133131 generic-types)]
134132 (info " Loaded" (count validators) " schema validators" )
148146 Throws a :bad-request service error if validation fails."
149147 [context concept]
150148 (let [concept-type (:concept-type concept)
151- metadata-spec (extract-concept-metadata-spec concept)
152- version (or (:version metadata-spec)
149+ version (or (:version (extract-concept-metadata-spec concept))
150+ ; ; If version is not specified in metadata, use the current version
151+ ; ; of the concept type, though it is likey that the concept failed
152+ ; ; schema validation and we never reach this point.
153153 (generics/current-generic-version concept-type))
154154
155155 ; ; Get validation functions
163163
164164 ; ; Throw service errors if any validation errors are found
165165 (when (seq errors)
166- (errors/throw-service-errors :bad-request errors))))
166+ (errors/throw-service-errors :invalid-data errors))))
0 commit comments