From 75ada05cf809a20758879eeafe7127c88e407a47 Mon Sep 17 00:00:00 2001 From: Tristan Penman Date: Sat, 6 Nov 2021 08:53:39 +1100 Subject: [PATCH] Use strong types in external_schema example, and update README --- README.md | 18 ++++++++++++++++++ examples/external_schema.cpp | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6794d8b..0d7e5d3 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,24 @@ Note that Valijson's `SchemaParser` and `Validator` classes expect you to pass i By default, Valijson classes will not throw exceptions (e.g. when failing to parse a schema). To enable exceptions for these cases, `VALIJSON_USE_EXCEPTIONS` must be defined. +### Strong vs Weak Types + +Valijson has a notion of strong and weak typing. By default, strong typing is used. For example, the following will create a validator that uses strong typing: + +```cpp +Validator validator; +``` + +This validator will not attempt to cast between types to satisfy a schema. So the string `"23"` will not be parsed as a number. + +Alternatively, weak typing can be used: + +```cpp +Validator validator(Validator::kWeakTypes); +``` + +This will create a validator that will attempt to cast values to satisfy a schema. The original motivation for this was to support the Boost Property Tree library, which can parse JSON, but stores values as strings. + ## Memory Management ## Valijson has been designed to safely manage, and eventually free, the memory that is allocated while parsing a schema or validating a document. When working with an externally loaded schema (i.e. one that is populated using the `SchemaParser` class) you can rely on RAII semantics. diff --git a/examples/external_schema.cpp b/examples/external_schema.cpp index d18a529..82d33da 100644 --- a/examples/external_schema.cpp +++ b/examples/external_schema.cpp @@ -57,7 +57,7 @@ int main(int argc, char *argv[]) } // Perform validation - Validator validator(Validator::kWeakTypes); + Validator validator(Validator::kStrongTypes); ValidationResults results; RapidJsonAdapter targetDocumentAdapter(targetDocument); if (!validator.validate(schema, targetDocumentAdapter, &results)) {