xref: /aosp_15_r20/external/icing/icing/util/document-validator.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ICING_UTIL_DOCUMENT_VALIDATOR_H_
16 #define ICING_UTIL_DOCUMENT_VALIDATOR_H_
17 
18 #include "icing/text_classifier/lib3/utils/base/status.h"
19 #include "icing/proto/document.pb.h"
20 #include "icing/schema/schema-store.h"
21 
22 namespace icing {
23 namespace lib {
24 
25 // This class validates DocumentProto based on the corresponding
26 // SchemaTypeConfigProto in the given type config map.
27 class DocumentValidator {
28  public:
29   explicit DocumentValidator(const SchemaStore* schema_store);
30   DocumentValidator() = delete;
31 
32   // This function validates:
33   //  1. DocumentProto.namespace is not empty
34   //  2. DocumentProto.uri is not empty in top-level documents. Nested documents
35   //     may have empty uris.
36   //  3. DocumentProto.schema is not empty
37   //  4. DocumentProto.schema matches one of SchemaTypeConfigProto.schema_type
38   //     in the given SchemaProto in constructor
39   //  5. Each PropertyProto.name in DocumentProto.properties is not empty
40   //  6. Each PropertyProto.name is unique
41   //  7. Each PropertyProto.name matches one of
42   //     PropertyConfigProto.property_name in the given SchemaProto in
43   //     constructor
44   //  8. For each PropertyProto, the size of repeated value field matches
45   //     PropertyConfigProto.cardinality defined in the given SchemaProto in
46   //     constructor (e.g. OPTIONAL means 0 or 1, REQUIRED means 1)
47   //  9. For each PropertyProto with nested DocumentProto,
48   //     DocumentProto.schema (nested) matches the current
49   //     PropertyConfigProto.schema_type
50   // 10. All PropertyProto with REQUIRED cardinality in the corresponding
51   //     PropertyConfigProto present in the DocumentProto
52   // 11. DocumentProto.score is not negative
53   // 12. DocumentProto.creation_timestamp_ms is not negative
54   // 13. DocumentProto.ttl_ms is not negative
55   //
56   // In addition, all nested DocumentProto will also be validated towards the
57   // requirements above.
58   //
59   // 'depth' indicates what nesting level the document may be at. A top-level
60   // document has a nesting depth of 0.
61   //
62   // Returns:
63   //   OK on success
64   //   FAILED_PRECONDITION if no schema is set yet
65   //   INVALID_ARGUMENT if any of case 1, 2, 3, 5, 8, 9, 10, 11, 12, 13 fails
66   //   NOT_FOUND if case 4 or 7 fails
67   //   ALREADY_EXISTS if case 6 fails
68   //   INTERNAL on any I/O error
69   libtextclassifier3::Status Validate(const DocumentProto& document,
70                                       int depth = 0);
71 
UpdateSchemaStore(const SchemaStore * schema_store)72   void UpdateSchemaStore(const SchemaStore* schema_store) {
73     schema_store_ = schema_store;
74   }
75 
76  private:
77   const SchemaStore* schema_store_;
78 };
79 
80 }  // namespace lib
81 }  // namespace icing
82 
83 #endif  // ICING_UTIL_DOCUMENT_VALIDATOR_H_
84