private (SCHEMA2 Model, Validation.ValidationResult Validation) _Read(BYTES jsonUtf8Bytes) { Guard.NotNull(jsonUtf8Bytes, nameof(jsonUtf8Bytes)); var root = new SCHEMA2(); var vcontext = new Validation.ValidationResult(root, this.Validation); var reader = new Utf8JsonReader(jsonUtf8Bytes); if (!reader.Read()) { vcontext.AddError(new Validation.ModelException(root, "Json is empty")); return(null, vcontext); } try { root.Deserialize(ref reader); root.OnDeserializationCompleted(); } catch (JsonException rex) { vcontext.AddError(new Validation.SchemaException(root, rex)); return(null, vcontext); } // schema validation root.ValidateReferences(vcontext.GetContext()); var ex = vcontext.Errors.FirstOrDefault(); if (ex != null) { return(null, vcontext); } // resolve external dependencies root._ResolveSatelliteDependencies(this); // full validation if (this.Validation != VALIDATIONMODE.Skip) { root.Validate(vcontext.GetContext()); ex = vcontext.Errors.FirstOrDefault(); if (ex != null) { return(null, vcontext); } } return(root, vcontext); }
/// <summary> /// This needs to be called immediately before writing to json, /// but immediately after preprocessing and buffer setup, so we have a valid model. /// </summary> /// <param name="model"></param> private void _ValidateBeforeWriting(SCHEMA2 model) { if (_NoCloneWatchdog) { return; } var vcontext = new Validation.ValidationResult(model, Validation.ValidationMode.Strict); model.ValidateReferences(vcontext.GetContext()); var ex = vcontext.Errors.FirstOrDefault(); if (ex != null) { throw ex; } model.Validate(vcontext.GetContext()); ex = vcontext.Errors.FirstOrDefault(); if (ex != null) { throw ex; } }
private (SCHEMA2 Model, Validation.ValidationResult Validation) _Read(ReadOnlyMemory <Byte> jsonUtf8Bytes) { var root = new SCHEMA2(); var vcontext = new Validation.ValidationResult(root, this.Validation); if (jsonUtf8Bytes.IsEmpty) { vcontext.AddError(new Validation.SchemaException(null, "JSon is empty.")); } var reader = new Utf8JsonReader(jsonUtf8Bytes.Span); try { if (!reader.Read()) { vcontext.AddError(new Validation.SchemaException(root, "Json is empty")); return(null, vcontext); } root.Deserialize(ref reader); root.OnDeserializationCompleted(); } catch (JsonException rex) { vcontext.AddError(new Validation.SchemaException(root, rex)); return(null, vcontext); } // binary chunk check foreach (var b in root.LogicalBuffers) { b.OnValidateBinaryChunk(vcontext.GetContext(root), this._BinaryChunk); } // schema validation root.ValidateReferences(vcontext.GetContext()); var ex = vcontext.Errors.FirstOrDefault(); if (ex != null) { return(null, vcontext); } // resolve external dependencies root._ResolveSatelliteDependencies(this); // full validation if (this.Validation != VALIDATIONMODE.Skip) { root.Validate(vcontext.GetContext()); ex = vcontext.Errors.FirstOrDefault(); if (ex != null) { return(null, vcontext); } } return(root, vcontext); }