public void WriteSchema(JSchema schema) { ValidationUtils.ArgumentNotNull(schema, nameof(schema)); _rootSchema = schema; _knownSchemas.Clear(); JSchemaDiscovery discovery; if (_externalSchemas != null) { foreach (ExternalSchema externalSchema in _externalSchemas) { discovery = new JSchemaDiscovery(schema, _knownSchemas, KnownSchemaState.External); discovery.Discover(externalSchema.Schema, externalSchema.Uri); } } if (_version == SchemaVersion.Unset) { _version = SchemaVersionHelpers.MapSchemaUri(schema.SchemaVersion); } discovery = new JSchemaDiscovery(schema, _knownSchemas, KnownSchemaState.InlinePending); discovery.Discover(schema, null); KnownSchema rootKnownSchema = _knownSchemas.Single(s => s.Schema == schema); rootKnownSchema.State = KnownSchemaState.Written; WriteSchemaInternal(schema); }
internal bool EnsureVersion(SchemaVersion minimum, SchemaVersion?maximum = null) { return(SchemaVersionHelpers.EnsureVersion(_version, minimum, maximum)); }
private void WriteSchemaObjectInternal(JSchema schema) { _writer.WriteStartObject(); if (schema == _rootSchema) { Uri resolvedVersionUri = (_version != SchemaVersion.Unset) ? SchemaVersionHelpers.MapSchemaVersion(_version) : schema.SchemaVersion; WritePropertyIfNotNull(_writer, Constants.PropertyNames.Schema, resolvedVersionUri); } if (EnsureVersion(SchemaVersion.Draft6)) { WritePropertyIfNotNull(_writer, Constants.PropertyNames.Id, schema.Id); } else { WritePropertyIfNotNull(_writer, Constants.PropertyNames.IdDraft4, schema.Id); } WritePropertyIfNotNull(_writer, Constants.PropertyNames.Anchor, schema.Anchor); WritePropertyIfNotNull(_writer, Constants.PropertyNames.Title, schema.Title); WritePropertyIfNotNull(_writer, Constants.PropertyNames.Description, schema.Description); if (schema._extensionData != null) { foreach (KeyValuePair <string, JToken> extensionDataPair in schema._extensionData) { bool isDefinitions = schema == _rootSchema && Constants.PropertyNames.IsDefinition(extensionDataPair.Key); _writer.WritePropertyName(extensionDataPair.Key); WriteToken(schema, _writer, extensionDataPair.Value, isDefinitions); } } if (schema.Type != null && schema.Type != JSchemaType.None) { WriteType(Constants.PropertyNames.Type, _writer, schema.Type.Value); } if (schema.Default != null) { _writer.WritePropertyName(Constants.PropertyNames.Default); schema.Default.WriteTo(_writer); } if (schema._allowAdditionalProperties != null) { _writer.WritePropertyName(Constants.PropertyNames.AdditionalProperties); _writer.WriteValue(schema._allowAdditionalProperties); } else if (schema.AdditionalProperties != null) { ReferenceOrWriteSchema(schema, schema.AdditionalProperties, Constants.PropertyNames.AdditionalProperties); } if (schema._allowAdditionalItems != null) { _writer.WritePropertyName(Constants.PropertyNames.AdditionalItems); _writer.WriteValue(schema._allowAdditionalItems); } else if (schema.AdditionalItems != null) { ReferenceOrWriteSchema(schema, schema.AdditionalItems, Constants.PropertyNames.AdditionalItems); } if (schema.AllowUnevaluatedProperties != null) { _writer.WritePropertyName(Constants.PropertyNames.UnevaluatedProperties); _writer.WriteValue(schema.AllowUnevaluatedProperties); } else if (schema.UnevaluatedProperties != null) { ReferenceOrWriteSchema(schema, schema.UnevaluatedProperties, Constants.PropertyNames.UnevaluatedProperties); } if (schema.AllowUnevaluatedItems != null) { _writer.WritePropertyName(Constants.PropertyNames.UnevaluatedItems); _writer.WriteValue(schema.AllowUnevaluatedItems); } else if (schema.UnevaluatedItems != null) { ReferenceOrWriteSchema(schema, schema.UnevaluatedItems, Constants.PropertyNames.UnevaluatedItems); } WriteSchemaDictionaryIfNotNull(schema, _writer, Constants.PropertyNames.Properties, schema._properties); WriteRequired(schema); WriteSchemaDictionaryIfNotNull(schema, _writer, Constants.PropertyNames.PatternProperties, schema._patternProperties); WriteItems(schema); WritePropertyIfNotDefault(_writer, Constants.PropertyNames.UniqueItems, schema.UniqueItems); if (EnsureVersion(SchemaVersion.Draft6)) { WritePropertyIfNotNull(_writer, schema.ExclusiveMinimum ? Constants.PropertyNames.ExclusiveMinimum : Constants.PropertyNames.Minimum, schema.Minimum); WritePropertyIfNotNull(_writer, schema.ExclusiveMaximum ? Constants.PropertyNames.ExclusiveMaximum : Constants.PropertyNames.Maximum, schema.Maximum); } else { WritePropertyIfNotNull(_writer, Constants.PropertyNames.Minimum, schema.Minimum); WritePropertyIfNotNull(_writer, Constants.PropertyNames.Maximum, schema.Maximum); WritePropertyIfNotDefault(_writer, Constants.PropertyNames.ExclusiveMinimum, schema.ExclusiveMinimum); WritePropertyIfNotDefault(_writer, Constants.PropertyNames.ExclusiveMaximum, schema.ExclusiveMaximum); } WritePropertyIfNotNull(_writer, Constants.PropertyNames.MinimumLength, schema.MinimumLength); WritePropertyIfNotNull(_writer, Constants.PropertyNames.MaximumLength, schema.MaximumLength); WritePropertyIfNotNull(_writer, Constants.PropertyNames.MinimumItems, schema.MinimumItems); WritePropertyIfNotNull(_writer, Constants.PropertyNames.MaximumItems, schema.MaximumItems); WritePropertyIfNotNull(_writer, Constants.PropertyNames.MinimumProperties, schema.MinimumProperties); WritePropertyIfNotNull(_writer, Constants.PropertyNames.MaximumProperties, schema.MaximumProperties); WritePropertyIfNotNull(_writer, Constants.PropertyNames.MinimumContains, schema.MinimumContains); WritePropertyIfNotNull(_writer, Constants.PropertyNames.MaximumContains, schema.MaximumContains); WritePropertyIfNotNull(_writer, Constants.PropertyNames.MultipleOf, schema.MultipleOf); WritePropertyIfNotNull(_writer, Constants.PropertyNames.Pattern, schema.Pattern); WritePropertyIfNotNull(_writer, Constants.PropertyNames.Format, schema.Format); if (EnsureVersion(SchemaVersion.Draft7)) { WritePropertyIfNotNull(_writer, Constants.PropertyNames.ContentEncoding, schema.ContentEncoding); WritePropertyIfNotNull(_writer, Constants.PropertyNames.ContentMediaType, schema.ContentMediaType); WritePropertyIfNotNull(_writer, Constants.PropertyNames.WriteOnly, schema.WriteOnly); WritePropertyIfNotNull(_writer, Constants.PropertyNames.ReadOnly, schema.ReadOnly); } if (!schema._enum.IsNullOrEmpty()) { _writer.WritePropertyName(Constants.PropertyNames.Enum); _writer.WriteStartArray(); foreach (JToken token in schema._enum) { token.WriteTo(_writer); } _writer.WriteEndArray(); } if (EnsureVersion(SchemaVersion.Draft6)) { if (schema.Const != null) { _writer.WritePropertyName(Constants.PropertyNames.Const); schema.Const.WriteTo(_writer); } WriteSchema(schema, schema.PropertyNames, Constants.PropertyNames.PropertyNamesSchema); WriteSchema(schema, schema.Contains, Constants.PropertyNames.Contains); } WriteSchemas(schema, schema._allOf, Constants.PropertyNames.AllOf); WriteSchemas(schema, schema._anyOf, Constants.PropertyNames.AnyOf); WriteSchemas(schema, schema._oneOf, Constants.PropertyNames.OneOf); WriteSchema(schema, schema.Not, Constants.PropertyNames.Not); if (EnsureVersion(SchemaVersion.Draft7)) { WriteSchema(schema, schema.If, Constants.PropertyNames.If); WriteSchema(schema, schema.Then, Constants.PropertyNames.Then); WriteSchema(schema, schema.Else, Constants.PropertyNames.Else); } WriteSchemaDictionaryIfNotNull(schema, _writer, Constants.PropertyNames.DependentSchemas, schema._dependentSchemas); if (!schema._dependentRequired.IsNullOrEmpty()) { _writer.WritePropertyName(Constants.PropertyNames.DependentRequired); _writer.WriteStartObject(); foreach (KeyValuePair <string, IList <string> > dependentRequired in schema._dependentRequired) { _writer.WritePropertyName(dependentRequired.Key); _writer.WriteStartArray(); for (int i = 0; i < dependentRequired.Value.Count; i++) { _writer.WriteValue(dependentRequired.Value[i]); } _writer.WriteEndArray(); } _writer.WriteEndObject(); } if (!schema._dependencies.IsNullOrEmpty()) { _writer.WritePropertyName(Constants.PropertyNames.Dependencies); _writer.WriteStartObject(); foreach (KeyValuePair <string, object> dependency in schema._dependencies) { if (dependency.Value is JSchema s) { ReferenceOrWriteSchema(schema, s, dependency.Key); } else if (dependency.Value is IList <string> a) { _writer.WritePropertyName(dependency.Key); _writer.WriteStartArray(); for (int i = 0; i < a.Count; i++) { _writer.WriteValue(a[i]); } _writer.WriteEndArray(); } } _writer.WriteEndObject(); } if (schema.Ref != null) { WriteReference(schema, schema.Ref, Constants.PropertyNames.Ref); } _writer.WriteEndObject(); }
private void WriteSchemaObjectInternal(JSchema schema) { _writer.WriteStartObject(); if (schema == _rootSchema) { Uri resolvedVersionUri = (_version != SchemaVersion.Unset) ? SchemaVersionHelpers.MapSchemaVersion(_version) : schema.SchemaVersion; WritePropertyIfNotNull(_writer, Constants.PropertyNames.Schema, resolvedVersionUri); } if (EnsureVersion(SchemaVersion.Draft6)) { WritePropertyIfNotNull(_writer, Constants.PropertyNames.Id, schema.Id); } else { WritePropertyIfNotNull(_writer, Constants.PropertyNames.IdDraft4, schema.Id); } WritePropertyIfNotNull(_writer, Constants.PropertyNames.Title, schema.Title); WritePropertyIfNotNull(_writer, Constants.PropertyNames.Description, schema.Description); if (schema._extensionData != null) { foreach (KeyValuePair <string, JToken> extensionDataPair in schema._extensionData) { _writer.WritePropertyName(extensionDataPair.Key); WriteToken(schema, _writer, extensionDataPair.Value); } } if (schema.Type != null) { WriteType(Constants.PropertyNames.Type, _writer, schema.Type.Value); } if (schema.Default != null) { _writer.WritePropertyName(Constants.PropertyNames.Default); schema.Default.WriteTo(_writer); } if (!schema.AllowAdditionalProperties) { _writer.WritePropertyName(Constants.PropertyNames.AdditionalProperties); _writer.WriteValue(schema.AllowAdditionalProperties); } else { if (schema.AdditionalProperties != null) { ReferenceOrWriteSchema(schema, schema.AdditionalProperties, Constants.PropertyNames.AdditionalProperties); } } if (!schema.AllowAdditionalItems) { _writer.WritePropertyName(Constants.PropertyNames.AdditionalItems); _writer.WriteValue(schema.AllowAdditionalItems); } else { if (schema.AdditionalItems != null) { ReferenceOrWriteSchema(schema, schema.AdditionalItems, Constants.PropertyNames.AdditionalItems); } } WriteSchemaDictionaryIfNotNull(schema, _writer, Constants.PropertyNames.Properties, schema._properties); WriteRequired(schema); WriteSchemaDictionaryIfNotNull(schema, _writer, Constants.PropertyNames.PatternProperties, schema._patternProperties); WriteItems(schema); WritePropertyIfNotDefault(_writer, Constants.PropertyNames.UniqueItems, schema.UniqueItems); if (EnsureVersion(SchemaVersion.Draft6)) { WritePropertyIfNotNull(_writer, schema.ExclusiveMinimum ? Constants.PropertyNames.ExclusiveMinimum : Constants.PropertyNames.Minimum, schema.Minimum); WritePropertyIfNotNull(_writer, schema.ExclusiveMaximum ? Constants.PropertyNames.ExclusiveMaximum : Constants.PropertyNames.Minimum, schema.Maximum); } else { WritePropertyIfNotNull(_writer, Constants.PropertyNames.Minimum, schema.Minimum); WritePropertyIfNotNull(_writer, Constants.PropertyNames.Maximum, schema.Maximum); WritePropertyIfNotDefault(_writer, Constants.PropertyNames.ExclusiveMinimum, schema.ExclusiveMinimum); WritePropertyIfNotDefault(_writer, Constants.PropertyNames.ExclusiveMaximum, schema.ExclusiveMaximum); } WritePropertyIfNotNull(_writer, Constants.PropertyNames.MinimumLength, schema.MinimumLength); WritePropertyIfNotNull(_writer, Constants.PropertyNames.MaximumLength, schema.MaximumLength); WritePropertyIfNotNull(_writer, Constants.PropertyNames.MinimumItems, schema.MinimumItems); WritePropertyIfNotNull(_writer, Constants.PropertyNames.MaximumItems, schema.MaximumItems); WritePropertyIfNotNull(_writer, Constants.PropertyNames.MinimumProperties, schema.MinimumProperties); WritePropertyIfNotNull(_writer, Constants.PropertyNames.MaximumProperties, schema.MaximumProperties); WritePropertyIfNotNull(_writer, Constants.PropertyNames.MultipleOf, schema.MultipleOf); WritePropertyIfNotNull(_writer, Constants.PropertyNames.Pattern, schema.Pattern); WritePropertyIfNotNull(_writer, Constants.PropertyNames.Format, schema.Format); if (!schema._enum.IsNullOrEmpty()) { _writer.WritePropertyName(Constants.PropertyNames.Enum); _writer.WriteStartArray(); foreach (JToken token in schema._enum) { token.WriteTo(_writer); } _writer.WriteEndArray(); } if (EnsureVersion(SchemaVersion.Draft6)) { if (schema.Const != null) { _writer.WritePropertyName(Constants.PropertyNames.Const); schema.Const.WriteTo(_writer); } WriteSchema(schema, schema.PropertyNames, Constants.PropertyNames.PropertyNamesSchema); WriteSchema(schema, schema.Contains, Constants.PropertyNames.Contains); } WriteSchemas(schema, schema._allOf, Constants.PropertyNames.AllOf); WriteSchemas(schema, schema._anyOf, Constants.PropertyNames.AnyOf); WriteSchemas(schema, schema._oneOf, Constants.PropertyNames.OneOf); WriteSchema(schema, schema.Not, Constants.PropertyNames.Not); _writer.WriteEndObject(); }