{"record":{"id":"09bd056b91a32ed1","repo":"JamesNK/Newtonsoft.Json","slug":"cannot-change-schema-while-validating-json","errorCode":null,"errorMessage":"Cannot change schema while validating JSON.","messagePattern":"Cannot change schema while validating JSON\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/JsonValidatingReader.cs","lineNumber":311,"sourceCode":"        public JsonValidatingReader(JsonReader reader)\n        {\n            ValidationUtils.ArgumentNotNull(reader, nameof(reader));\n            _reader = reader;\n            _stack = new Stack<SchemaScope>();\n        }\n\n        /// <summary>\n        /// Gets or sets the schema.\n        /// </summary>\n        /// <value>The schema.</value>\n        public JsonSchema Schema\n        {\n            get => _schema;\n            set\n            {\n                if (TokenType != JsonToken.None)\n                {\n                    throw new InvalidOperationException(\"Cannot change schema while validating JSON.\");\n                }\n\n                _schema = value;\n                _model = null;\n            }\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"JsonReader\"/> used to construct this <see cref=\"JsonValidatingReader\"/>.\n        /// </summary>\n        /// <value>The <see cref=\"JsonReader\"/> specified in the constructor.</value>\n        public JsonReader Reader => _reader;\n\n        /// <summary>\n        /// Changes the reader's state to <see cref=\"JsonReader.State.Closed\"/>.\n        /// If <see cref=\"JsonReader.CloseInput\"/> is set to <c>true</c>, the underlying <see cref=\"JsonReader\"/> is also closed.\n        /// </summary>\n        public override void Close()","sourceCodeStart":293,"sourceCodeEnd":329,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/JsonValidatingReader.cs#L293-L329","documentation":"Thrown by the JsonValidatingReader.Schema setter when the schema is changed after validation has started (i.e. TokenType is no longer JsonToken.None). Changing the schema mid-stream would invalidate the in-progress validation state (scopes, current model), so it is rejected with InvalidOperationException. The schema must be set before any Read() call.","triggerScenarios":"Assigning reader.Schema = newSchema after at least one Read() has advanced the token past None; reassigning the schema per-iteration inside a read loop.","commonSituations":"Building the validating reader, reading a few tokens, then assigning the schema; lazy/deferred schema loading that assigns after first read; copying/reusing a reader instance for multiple schemas without recreating it.","solutions":["Set Schema immediately after constructing JsonValidatingReader, before calling Read().","Pass the schema via the JsonValidatingReader(JsonReader, JsonSchema) constructor to enforce ordering.","Create a fresh JsonValidatingReader per schema rather than mutating an active one."],"exampleFix":"// before\nvar validating = new JsonValidatingReader(textReader);\nwhile (validating.Read()) { /* ... */ }\nvalidating.Schema = otherSchema; // throws\n\n// after\nvar validating = new JsonValidatingReader(textReader, otherSchema);","handlingStrategy":"validation","validationCode":"if (validating.TokenType == JsonToken.None)\n{\n    validating.Schema = schema;\n}","typeGuard":"static bool CanSetSchema(JsonValidatingReader r) => r.TokenType == JsonToken.None;","tryCatchPattern":null,"preventionTips":["Set Schema (or pass via constructor) before the first Read().","Create a new JsonValidatingReader per schema.","Treat the schema as immutable once validation starts."],"tags":["schema-validation","configuration","invalid-operation"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}