generateJsonSchema function

StringBuffer generateJsonSchema(
  1. SchemaType<Object?> schemaType, {
  2. String? schemaUri = jsonSchema_2020_12,
  3. String? schemaId,
  4. JsonSchemaOptions options = const JsonSchemaOptions(),
})

Generate a JSON Schema where the given schemaType is the root type.

An optional schemaId may be given and is the value of $id unless set to null.

The default schemaUri is jsonSchema_2020_12 but may be overridden, including setting it to null so $schema will not be added to the result. Notice that if another value is given, it makes no difference to how the rest of the schema is generated (i.e. this method cannot generate schemas for other JSON Schema versions).

See jsonSchemaValidatorGenerators.

Implementation

StringBuffer generateJsonSchema(SchemaType<Object?> schemaType,
    {String? schemaUri = jsonSchema_2020_12,
    String? schemaId,
    JsonSchemaOptions options = const JsonSchemaOptions()}) {
  final buffer = StringBuffer();
  if (options.startObject) {
    buffer.write('{ ');
  }
  if (schemaUri != null) {
    buffer.write(r'"$schema": ');
    buffer.writeJson(schemaUri);
  }
  if (schemaId != null) {
    buffer.write(r', "$id": ');
    buffer.writeJson(schemaId);
  }
  if (schemaUri != null || schemaId != null) {
    buffer.write(', ');
  }

  _generate(schemaType, buffer, null, options.copyWith(startObject: false));

  return buffer;
}