• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/proto/schema.pb.h"
21 #include "icing/schema/schema-store.h"
22 
23 namespace icing {
24 namespace lib {
25 
26 // This class validates DocumentProto based on the corresponding
27 // SchemaTypeConfigProto in the given type config map.
28 class DocumentValidator {
29  public:
30   explicit DocumentValidator(const SchemaStore* schema_store);
31   DocumentValidator() = delete;
32 
33   // This function validates:
34   //  1. DocumentProto.namespace is not empty
35   //  2. DocumentProto.uri is not empty in top-level documents. Nested documents
36   //     may have empty uris.
37   //  3. DocumentProto.schema is not empty
38   //  4. DocumentProto.schema matches one of SchemaTypeConfigProto.schema_type
39   //     in the given SchemaProto in constructor
40   //  5. Each PropertyProto.name in DocumentProto.properties is not empty
41   //  6. Each PropertyProto.name is unique
42   //  7. Each PropertyProto.name matches one of
43   //     PropertyConfigProto.property_name in the given SchemaProto in
44   //     constructor
45   //  8. For each PropertyProto, the size of repeated value field matches
46   //     PropertyConfigProto.cardinality defined in the given SchemaProto in
47   //     constructor (e.g. OPTIONAL means 0 or 1, REQUIRED means 1)
48   //  9. For each PropertyProto with nested DocumentProto,
49   //     DocumentProto.schema (nested) matches the current
50   //     PropertyConfigProto.schema_type
51   // 10. All PropertyProto with REQUIRED cardinality in the corresponding
52   //     PropertyConfigProto present in the DocumentProto
53   // 11. DocumentProto.score is not negative
54   // 12. DocumentProto.creation_timestamp_ms is not negative
55   // 13. DocumentProto.ttl_ms is not negative
56   //
57   // In addition, all nested DocumentProto will also be validated towards the
58   // requirements above.
59   //
60   // 'depth' indicates what nesting level the document may be at. A top-level
61   // document has a nesting depth of 0.
62   //
63   // Returns:
64   //   OK on success
65   //   FAILED_PRECONDITION if no schema is set yet
66   //   INVALID_ARGUMENT if any of case 1, 2, 3, 5, 8, 9, 10, 11, 12, 13 fails
67   //   NOT_FOUND if case 4 or 7 fails
68   //   ALREADY_EXISTS if case 6 fails
69   //   INTERNAL on any I/O error
70   libtextclassifier3::Status Validate(const DocumentProto& document,
71                                       int depth = 0);
72 
UpdateSchemaStore(const SchemaStore * schema_store)73   void UpdateSchemaStore(const SchemaStore* schema_store) {
74     schema_store_ = schema_store;
75   }
76 
77  private:
78   const SchemaStore* schema_store_;
79 };
80 
81 }  // namespace lib
82 }  // namespace icing
83 
84 #endif  // ICING_UTIL_DOCUMENT_VALIDATOR_H_
85