Implementation:Alibaba MNN RapidJSON Schema
| Knowledge Sources | |
|---|---|
| Domains | JSON_Parsing, Schema_Validation |
| Last Updated | 2026-02-10 12:00 GMT |
Overview
3rd_party/rapidjson/schema.h (2496 lines) implements JSON Schema validation (Draft v4) for the vendored RapidJSON library within Alibaba MNN. It provides compile-time schema document construction and runtime validation of JSON values against a schema. The validator follows the SAX handler pattern, enabling both DOM-based and streaming validation.
Usage note: Vendored dependency used internally by MNN for JSON configuration parsing (model configs, LLM configs). Not directly imported by end users.
Key Classes
GenericSchemaDocument
Compiles a JSON document into an internal schema representation:
template <typename ValueT, typename Allocator = CrtAllocator>
class GenericSchemaDocument {
public:
typedef ValueT ValueType;
typedef IGenericRemoteSchemaDocumentProvider<GenericSchemaDocument> IRemoteSchemaDocumentProviderType;
GenericSchemaDocument(const ValueType& document,
IRemoteSchemaDocumentProviderType* remoteProvider = 0,
Allocator* allocator = 0);
~GenericSchemaDocument();
const SchemaType& GetRoot() const;
};
GenericSchemaValidator
Validates JSON data against a compiled schema. It implements the SAX Handler interface, so it can validate during parsing or accept events from DOM traversal:
template <typename SchemaDocumentType,
typename OutputHandler = BaseReaderHandler<typename SchemaDocumentType::SchemaType::EncodingType>,
typename StateAllocator = CrtAllocator>
class GenericSchemaValidator :
public internal::ISchemaStateFactory<typename SchemaDocumentType::SchemaType>,
public internal::ISchemaValidator,
public internal::IValidationErrorHandler<typename SchemaDocumentType::SchemaType>
{
public:
GenericSchemaValidator(const SchemaDocumentType& schemaDocument,
StateAllocator* allocator = 0,
size_t schemaStackCapacity = kDefaultSchemaStackCapacity,
size_t documentStackCapacity = kDefaultDocumentStackCapacity);
// SAX handler methods
bool Null();
bool Bool(bool b);
bool Int(int i);
bool Uint(unsigned u);
bool Int64(int64_t i);
bool Uint64(uint64_t u);
bool Double(double d);
bool String(const Ch* str, SizeType length, bool copy);
bool StartObject();
bool Key(const Ch* str, SizeType length, bool copy);
bool EndObject(SizeType memberCount);
bool StartArray();
bool EndArray(SizeType elementCount);
// Validation result
bool IsValid() const;
void Reset();
StringRefType GetInvalidSchemaKeyword() const;
};
The default typedef is:
typedef GenericSchemaDocument<Value> SchemaDocument;
typedef GenericSchemaValidator<SchemaDocument> SchemaValidator;
Supported Schema Keywords
The validator supports the following JSON Schema Draft v4 keywords:
| Category | Keywords |
|---|---|
| Type validation | type, enum
|
| Numeric constraints | multipleOf, minimum, maximum, exclusiveMinimum, exclusiveMaximum
|
| String constraints | minLength, maxLength, pattern
|
| Array constraints | items, additionalItems, minItems, maxItems, uniqueItems
|
| Object constraints | properties, additionalProperties, required, minProperties, maxProperties, patternProperties, dependencies
|
| Composition | allOf, anyOf, oneOf, not
|
| References | $ref (with IRemoteSchemaDocumentProvider for remote references)
|
Regex Support
The pattern keyword requires regex matching. The header automatically selects an engine:
- Internal regex (default) -- uses
internal/regex.hwhenRAPIDJSON_SCHEMA_USE_INTERNALREGEXis enabled - std::regex -- uses
<regex>whenRAPIDJSON_SCHEMA_USE_STDREGEXis enabled and C++11 is available
#if !defined(RAPIDJSON_SCHEMA_USE_INTERNALREGEX)
#define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 1
#else
#define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 0
#endif
Dependencies
document.h-- DOM value types for schema document inputpointer.h-- JSON Pointer for schema$refresolution and error path reportingstringbuffer.h-- String buffer for internal operationsinternal/regex.h-- Lightweight regex engine (when using internal regex)<cmath>--abs(),floor()for numeric validation
License
MIT License. Copyright (C) 2015 THL A29 Limited (Tencent) and Milo Yip.
See Also
- Alibaba_MNN_RapidJSON_Document -- DOM document validated by schema
- Alibaba_MNN_RapidJSON_Pointer -- JSON Pointer used for $ref resolution
- Alibaba_MNN_RapidJSON_Regex -- Internal regex engine for pattern validation
- Alibaba_MNN_RapidJSON_Reader -- SAX reader that can validate during parsing