...

Source file src/cuelang.org/go/encoding/jsonschema/jsonschema.go

Documentation: cuelang.org/go/encoding/jsonschema

     1  // Copyright 2020 CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package jsonschema implements the JSON schema standard.
    16  //
    17  // # Mapping and Linking
    18  //
    19  // JSON Schema are often defined in a single file. CUE, on the other hand
    20  // idiomatically defines schema as a definition.
    21  //
    22  // CUE:
    23  //
    24  //	$schema: which schema is used for validation.
    25  //	$id: which validation does this schema provide.
    26  //
    27  //	Foo: _ @jsonschema(sc)
    28  //	@source(https://...) // What schema is used to validate.
    29  //
    30  // NOTE: JSON Schema is a draft standard and may undergo backwards incompatible
    31  // changes.
    32  package jsonschema
    33  
    34  import (
    35  	"cuelang.org/go/cue"
    36  	"cuelang.org/go/cue/ast"
    37  	"cuelang.org/go/cue/token"
    38  )
    39  
    40  // Extract converts JSON Schema data into an equivalent CUE representation.
    41  //
    42  // The generated CUE schema is guaranteed to deem valid any value that is
    43  // a valid instance of the source JSON schema.
    44  func Extract(data cue.InstanceOrValue, cfg *Config) (f *ast.File, err error) {
    45  	d := &decoder{cfg: cfg}
    46  
    47  	f = d.decode(data.Value())
    48  	if d.errs != nil {
    49  		return nil, d.errs
    50  	}
    51  	return f, nil
    52  }
    53  
    54  // A Config configures a JSON Schema encoding or decoding.
    55  type Config struct {
    56  	PkgName string
    57  
    58  	// ID sets the URL of the original source, corresponding to the $id field.
    59  	ID string
    60  
    61  	// JSON reference of location containing schema. The empty string indicates
    62  	// that there is a single schema at the root.
    63  	//
    64  	// Examples:
    65  	//  "#/"                     top-level fields are schemas.
    66  	//  "#/components/schemas"   the canonical OpenAPI location.
    67  	Root string
    68  
    69  	// Map maps the locations of schemas and definitions to a new location.
    70  	// References are updated accordingly. A returned label must be
    71  	// an identifier or string literal.
    72  	//
    73  	// The default mapping is
    74  	//    {}                     {}
    75  	//    {"definitions", foo}   {#foo} or {#, foo}
    76  	//    {"$defs", foo}         {#foo} or {#, foo}
    77  	Map func(pos token.Pos, path []string) ([]ast.Label, error)
    78  
    79  	// TODO: configurability to make it compatible with OpenAPI, such as
    80  	// - locations of definitions: #/components/schemas, for instance.
    81  	// - selection and definition of formats
    82  	// - documentation hooks.
    83  
    84  	// Strict reports an error for unsupported features, rather than ignoring
    85  	// them.
    86  	Strict bool
    87  
    88  	_ struct{} // prohibit casting from different type.
    89  }
    90  

View as plain text