cuelang.org/go@v0.10.1/encoding/jsonschema/jsonschema.go (about) 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 "net/url" 36 37 "cuelang.org/go/cue" 38 "cuelang.org/go/cue/ast" 39 "cuelang.org/go/cue/token" 40 ) 41 42 // Extract converts JSON Schema data into an equivalent CUE representation. 43 // 44 // The generated CUE schema is guaranteed to deem valid any value that is 45 // a valid instance of the source JSON schema. 46 func Extract(data cue.InstanceOrValue, cfg *Config) (f *ast.File, err error) { 47 if cfg.MapURL == nil { 48 cfg1 := *cfg 49 cfg = &cfg1 50 cfg1.MapURL = DefaultMapURL 51 } 52 d := &decoder{ 53 cfg: cfg, 54 mapURLErrors: make(map[string]bool), 55 } 56 57 f = d.decode(data.Value()) 58 if d.errs != nil { 59 return nil, d.errs 60 } 61 return f, nil 62 } 63 64 // A Config configures a JSON Schema encoding or decoding. 65 type Config struct { 66 PkgName string 67 68 // ID sets the URL of the original source, corresponding to the $id field. 69 ID string 70 71 // JSON reference of location containing schema. The empty string indicates 72 // that there is a single schema at the root. 73 // 74 // Examples: 75 // "#/" top-level fields are schemas. 76 // "#/components/schemas" the canonical OpenAPI location. 77 Root string 78 79 // Map maps the locations of schemas and definitions to a new location. 80 // References are updated accordingly. A returned label must be 81 // an identifier or string literal. 82 // 83 // The default mapping is 84 // {} {} 85 // {"definitions", foo} {#foo} or {#, foo} 86 // {"$defs", foo} {#foo} or {#, foo} 87 Map func(pos token.Pos, path []string) ([]ast.Label, error) 88 89 // MapURL maps a URL reference as found in $ref to 90 // an import path for a package. 91 // If this is nil, [DefaultMapURL] will be used. 92 MapURL func(u *url.URL) (importPath string, err error) 93 94 // TODO: configurability to make it compatible with OpenAPI, such as 95 // - locations of definitions: #/components/schemas, for instance. 96 // - selection and definition of formats 97 // - documentation hooks. 98 99 // Strict reports an error for unsupported features, rather than ignoring 100 // them. 101 Strict bool 102 103 _ struct{} // prohibit casting from different type. 104 }