data.jsonschema #
JSON Schema
A V library for the JSON Schema model, and a few handy functions.
JSON Schema Model
Defined here, "JSON Schema is a declarative language that allows you to annotate and validate JSON documents." The model in this module provides a struct that can easily be encoded into a JSON Schema.
Generating a Schema
The generate.v file provides functions that can generate JSONSchema from codemodels. This allows for easy generation of JSON Schema from structs, and is useful for generating schemas from parsed code in v.
Example:
struct_ := codemodel.Struct {
name: 'Mystruct'
fields: [
codemodel.StructField {
name: 'myfield'
typ: 'string'
}
]
}
schema := struct_to_schema(struct_)
Generating Schemas for Anonymous Structs
The properties of a JSON Schema is a list of key value pairs, where keys represent the subschema's name and the value is the schema (or the reference to the schema which is defined elsewhere) of the property. This is analogous to the fields of a struct, which is represented by a field name and a type.
It's good practice to define object type schemas separately and reference them in properties, especially if the same schema is used in multiple places. However, object type schemas can also be defined in property definitions. This may make sense if the schema is exclusively used as a property of a schema, similar to using an anonymous struct for the type definition of a field of a struct.
As such, schema's generated from structs that declare anonymous structs as field types, include a schema definition in the property field.
Notes
As this issue is still not resolved, a json schema cannot be decoded into the json schema structure defined in this module. As such, to decode json schema string into a structure the pub fn decode(data str) !Schema
function defined in decode.v
must be used.
fn decode #
fn decode(data string) !Schema
fn decode_items #
fn decode_items(data Any) !Items
fn decode_schemaref #
fn decode_schemaref(data_map map[string]Any) !SchemaRef
fn decode_schemaref_map #
fn decode_schemaref_map(data_map map[string]Any) !map[string]SchemaRef
fn param_to_schema #
fn param_to_schema(param Param) SchemaRef
fn result_to_schema #
fn result_to_schema(result Result) SchemaRef
fn struct_to_schema #
fn struct_to_schema(struct_ Struct) SchemaRef
struct_to_schema generates a json schema or reference from a struct model
fn sumtype_to_schema #
fn sumtype_to_schema(sumtype codemodel.Sumtype) SchemaRef
struct_to_schema generates a json schema or reference from a struct model
fn type_to_schema #
fn type_to_schema(typ Type) SchemaRef
fn typesymbol_to_schema #
fn typesymbol_to_schema(symbol_ string) SchemaRef
typesymbol_to_schema receives a typesymbol, if the typesymbol belongs to a user defined struct it returns a reference to the schema, else it returns a schema for the typesymbol
fn (SchemaRef) to_code #
fn (sr SchemaRef) to_code() !Type
fn (SchemaRef) to_struct_field #
fn (schema SchemaRef) to_struct_field(name string) !StructField
struct Reference #
struct Reference {
pub:
ref string @[json: 'ref']
}
fn (Reference) to_type #
fn (ref Reference) to_type() Type
fn (Reference) to_type_symbol #
fn (ref Reference) to_type_symbol() string
struct Schema #
struct Schema {
pub mut:
schema string @[json: 'schema']
id string @[json: 'id']
title string
description string
typ string @[json: 'type']
properties map[string]SchemaRef
additional_properties SchemaRef @[json: 'additionalProperties']
required []string
ref string
items Items
defs map[string]SchemaRef
one_of []SchemaRef @[json: 'oneOf']
// todo: make fields optional upon the fixing of https://github.com/vlang/v/issues/18775
// from https://git.sr.ht/~emersion/go-jsonschema/tree/master/item/schema.go
// Validation for numbers
multiple_of int @[json: 'multipleOf'; omitempty]
maximum int @[omitempty]
exclusive_maximum int @[json: 'exclusiveMaximum'; omitempty]
minimum int @[omitempty]
exclusive_minimum int @[json: 'exclusiveMinimum'; omitempty]
}
https://json-schema.org/draft-07/json-schema-release-notes.html
fn (Schema) to_code #
fn (schema Schema) to_code() !CodeItem
fn (Schema) to_struct #
fn (schema Schema) to_struct() !Struct
fn (Schema) v_encode #
fn (schema Schema) v_encode() !string
fn (Schema) vstructs_encode #
fn (schema Schema) vstructs_encode() ![]string
vstructs_encode encodes a schema into V structs. if a schema has nested object type schemas or defines object type schemas, recrusively encodes object type schemas and pushes to the array of structs. returns an array of schemas that have been encoded into V structs.
fn (Schema) vtype_encode #
fn (schema Schema) vtype_encode() !string
code_type generates a typesymbol for the schema