Marcel van Lohuizen | 47d9870 | 2020-01-17 21:51:39 +0100 | [diff] [blame] | 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 |
Marcel van Lohuizen | 56c994d | 2020-04-21 16:20:36 +0200 | [diff] [blame] | 20 | // idiomatically defines schema as a definition. |
Marcel van Lohuizen | 47d9870 | 2020-01-17 21:51:39 +0100 | [diff] [blame] | 21 | // |
| 22 | // CUE: |
| 23 | // $schema: which schema is used for validation. |
| 24 | // $id: which validation does this schema provide. |
| 25 | // |
| 26 | // Foo: _ @jsonschema(sc) |
| 27 | // @source(https://...) // What schema is used to validate. |
| 28 | // |
| 29 | // NOTE: JSON Schema is a draft standard and may undergo backwards incompatible |
| 30 | // changes. |
| 31 | package jsonschema |
| 32 | |
| 33 | import ( |
Marcel van Lohuizen | 845df05 | 2020-07-26 13:15:45 +0200 | [diff] [blame] | 34 | "cuelang.org/go/cue" |
Marcel van Lohuizen | 47d9870 | 2020-01-17 21:51:39 +0100 | [diff] [blame] | 35 | "cuelang.org/go/cue/ast" |
Marcel van Lohuizen | 671b956 | 2020-03-10 12:42:45 +0100 | [diff] [blame] | 36 | "cuelang.org/go/cue/token" |
Marcel van Lohuizen | 47d9870 | 2020-01-17 21:51:39 +0100 | [diff] [blame] | 37 | ) |
| 38 | |
| 39 | // Extract converts JSON Schema data into an equivalent CUE representation. |
| 40 | // |
| 41 | // The generated CUE schema is guaranteed to deem valid any value that is |
| 42 | // a valid instance of the source JSON schema. |
Marcel van Lohuizen | 671b956 | 2020-03-10 12:42:45 +0100 | [diff] [blame] | 43 | func Extract(data *cue.Instance, cfg *Config) (f *ast.File, err error) { |
Marcel van Lohuizen | 53e5561 | 2020-05-19 14:32:16 +0200 | [diff] [blame] | 44 | d := &decoder{cfg: cfg} |
Marcel van Lohuizen | 671b956 | 2020-03-10 12:42:45 +0100 | [diff] [blame] | 45 | |
| 46 | f = d.decode(data.Value()) |
Marcel van Lohuizen | 47d9870 | 2020-01-17 21:51:39 +0100 | [diff] [blame] | 47 | if d.errs != nil { |
| 48 | return nil, d.errs |
| 49 | } |
Marcel van Lohuizen | 671b956 | 2020-03-10 12:42:45 +0100 | [diff] [blame] | 50 | return f, nil |
Marcel van Lohuizen | 47d9870 | 2020-01-17 21:51:39 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // A Config configures a JSON Schema encoding or decoding. |
| 54 | type Config struct { |
Marcel van Lohuizen | bd98b13 | 2020-03-05 15:38:21 +0100 | [diff] [blame] | 55 | PkgName string |
| 56 | |
Marcel van Lohuizen | 0059b2b | 2020-04-14 15:31:11 +0200 | [diff] [blame] | 57 | // ID sets the URL of the original source, corresponding to the $id field. |
| 58 | ID string |
Marcel van Lohuizen | 47d9870 | 2020-01-17 21:51:39 +0100 | [diff] [blame] | 59 | |
Marcel van Lohuizen | 671b956 | 2020-03-10 12:42:45 +0100 | [diff] [blame] | 60 | // JSON reference of location containing schema. The empty string indicates |
| 61 | // that there is a single schema at the root. |
| 62 | // |
| 63 | // Examples: |
| 64 | // "#/" top-level fields are schemas. |
| 65 | // "#/components/schemas" the canonical OpenAPI location. |
| 66 | Root string |
| 67 | |
| 68 | // Map maps the locations of schemas and definitions to a new location. |
Marcel van Lohuizen | 435989a | 2020-05-06 18:43:58 +0200 | [diff] [blame] | 69 | // References are updated accordingly. A returned label must be |
| 70 | // an identifier or string literal. |
Marcel van Lohuizen | 671b956 | 2020-03-10 12:42:45 +0100 | [diff] [blame] | 71 | // |
| 72 | // The default mapping is |
Marcel van Lohuizen | 435989a | 2020-05-06 18:43:58 +0200 | [diff] [blame] | 73 | // {} {} |
Marcel van Lohuizen | dbf1c00 | 2020-05-16 14:19:34 +0200 | [diff] [blame] | 74 | // {"definitions", foo} {#foo} or {#, foo} |
| 75 | // {"$defs", foo} {#foo} or {#, foo} |
Marcel van Lohuizen | 435989a | 2020-05-06 18:43:58 +0200 | [diff] [blame] | 76 | Map func(pos token.Pos, path []string) ([]ast.Label, error) |
Marcel van Lohuizen | 671b956 | 2020-03-10 12:42:45 +0100 | [diff] [blame] | 77 | |
Marcel van Lohuizen | 47d9870 | 2020-01-17 21:51:39 +0100 | [diff] [blame] | 78 | // TODO: configurability to make it compatible with OpenAPI, such as |
| 79 | // - locations of definitions: #/components/schemas, for instance. |
| 80 | // - selection and definition of formats |
| 81 | // - documentation hooks. |
| 82 | |
Marcel van Lohuizen | 0059b2b | 2020-04-14 15:31:11 +0200 | [diff] [blame] | 83 | // Strict reports an error for unsupported features, rather than ignoring |
| 84 | // them. |
| 85 | Strict bool |
| 86 | |
Marcel van Lohuizen | 47d9870 | 2020-01-17 21:51:39 +0100 | [diff] [blame] | 87 | _ struct{} // prohibit casting from different type. |
| 88 | } |