|
| 1 | +// Package schema contains code for working with JSON schema. |
| 2 | +package schema |
| 3 | + |
| 4 | +import ( |
| 5 | + "net/url" |
| 6 | + "path/filepath" |
| 7 | + "regexp" |
| 8 | + |
| 9 | + "github.com/arduino/go-paths-helper" |
| 10 | + "github.com/xeipuuv/gojsonschema" |
| 11 | +) |
| 12 | + |
| 13 | +// Compile compiles the schema files specified by the filename arguments and returns the compiled schema. |
| 14 | +func Compile(schemaFilename string, referencedSchemaFilenames []string, schemasPath *paths.Path) *gojsonschema.Schema { |
| 15 | + schemaLoader := gojsonschema.NewSchemaLoader() |
| 16 | + |
| 17 | + // Load the referenced schemas. |
| 18 | + for _, referencedSchemaFilename := range referencedSchemaFilenames { |
| 19 | + referencedSchemaPath := schemasPath.Join(referencedSchemaFilename) |
| 20 | + referencedSchemaURI := pathURI(referencedSchemaPath) |
| 21 | + err := schemaLoader.AddSchemas(gojsonschema.NewReferenceLoader(referencedSchemaURI)) |
| 22 | + if err != nil { |
| 23 | + panic(err.Error()) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + // Compile the schema. |
| 28 | + schemaPath := schemasPath.Join(schemaFilename) |
| 29 | + schemaURI := pathURI(schemaPath) |
| 30 | + compiledSchema, err := schemaLoader.Compile(gojsonschema.NewReferenceLoader(schemaURI)) |
| 31 | + if err != nil { |
| 32 | + panic(err.Error()) |
| 33 | + } |
| 34 | + return compiledSchema |
| 35 | +} |
| 36 | + |
| 37 | +// Validate validates an instance against a JSON schema and returns the gojsonschema.Result object. |
| 38 | +func Validate(instanceObject interface{}, schemaObject *gojsonschema.Schema) *gojsonschema.Result { |
| 39 | + result, err := schemaObject.Validate(gojsonschema.NewGoLoader(instanceObject)) |
| 40 | + if err != nil { |
| 41 | + panic(err.Error()) |
| 42 | + } |
| 43 | + |
| 44 | + return result |
| 45 | +} |
| 46 | + |
| 47 | +// RequiredPropertyMissing returns whether the given required property is missing from the document. |
| 48 | +func RequiredPropertyMissing(propertyName string, validationResult *gojsonschema.Result) bool { |
| 49 | + return ValidationErrorMatch("required", "(root)", propertyName+" is required", validationResult) |
| 50 | +} |
| 51 | + |
| 52 | +// PropertyPatternMismatch returns whether the given property did not match the regular expression defined in the JSON schema. |
| 53 | +func PropertyPatternMismatch(propertyName string, validationResult *gojsonschema.Result) bool { |
| 54 | + return ValidationErrorMatch("pattern", propertyName, "", validationResult) |
| 55 | +} |
| 56 | + |
| 57 | +// ValidationErrorMatch returns whether the given query matches against the JSON schema validation error. |
| 58 | +// See: https://github.com/xeipuuv/gojsonschema#working-with-errors |
| 59 | +func ValidationErrorMatch(typeQuery string, fieldQuery string, descriptionQueryRegexp string, validationResult *gojsonschema.Result) bool { |
| 60 | + if validationResult.Valid() { |
| 61 | + // No error, so nothing to match |
| 62 | + return false |
| 63 | + } |
| 64 | + for _, validationError := range validationResult.Errors() { |
| 65 | + if typeQuery == "" || typeQuery == validationError.Type() { |
| 66 | + if fieldQuery == "" || fieldQuery == validationError.Field() { |
| 67 | + descriptionQuery := regexp.MustCompile(descriptionQueryRegexp) |
| 68 | + return descriptionQuery.MatchString(validationError.Description()) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return false |
| 74 | +} |
| 75 | + |
| 76 | +// pathURI returns the URI representation of the path argument. |
| 77 | +func pathURI(path *paths.Path) string { |
| 78 | + uriFriendlyPath := filepath.ToSlash(path.String()) |
| 79 | + pathURI := url.URL{ |
| 80 | + Scheme: "file", |
| 81 | + Path: uriFriendlyPath, |
| 82 | + } |
| 83 | + |
| 84 | + return pathURI.String() |
| 85 | +} |
0 commit comments