|
| 1 | +// Package schema contains code for working with JSON schema. |
| 2 | +package schema |
| 3 | + |
| 4 | +import ( |
| 5 | + "os" |
| 6 | + "regexp" |
| 7 | + |
| 8 | + "github.com/arduino/arduino-check/util" |
| 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) *gojsonschema.Schema { |
| 15 | + workingPath, _ := os.Getwd() |
| 16 | + schemasPath := paths.New(workingPath) |
| 17 | + |
| 18 | + schemaLoader := gojsonschema.NewSchemaLoader() |
| 19 | + |
| 20 | + // Load the referenced schemas. |
| 21 | + for _, referencedSchemaFilename := range referencedSchemaFilenames { |
| 22 | + referencedSchemaPath := schemasPath.Join(referencedSchemaFilename) |
| 23 | + referencedSchemaURI := util.PathURI(referencedSchemaPath) |
| 24 | + err := schemaLoader.AddSchemas(gojsonschema.NewReferenceLoader(referencedSchemaURI)) |
| 25 | + if err != nil { |
| 26 | + panic(err.Error()) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // Compile the schema. |
| 31 | + schemaPath := schemasPath.Join(schemaFilename) |
| 32 | + schemaURI := util.PathURI(schemaPath) |
| 33 | + compiledSchema, err := schemaLoader.Compile(gojsonschema.NewReferenceLoader(schemaURI)) |
| 34 | + if err != nil { |
| 35 | + panic(err.Error()) |
| 36 | + } |
| 37 | + return compiledSchema |
| 38 | +} |
| 39 | + |
| 40 | +// Validate validates an instance against a JSON schema and returns the gojsonschema.Result object. |
| 41 | +func Validate(instanceObject interface{}, schemaObject *gojsonschema.Schema) *gojsonschema.Result { |
| 42 | + result, err := schemaObject.Validate(gojsonschema.NewGoLoader(instanceObject)) |
| 43 | + if err != nil { |
| 44 | + panic(err.Error()) |
| 45 | + } |
| 46 | + |
| 47 | + return result |
| 48 | +} |
| 49 | + |
| 50 | +// RequiredPropertyMissing returns whether the given required property is missing from the document. |
| 51 | +func RequiredPropertyMissing(propertyName string, validationResult *gojsonschema.Result) bool { |
| 52 | + return ValidationErrorMatch("required", "(root)", propertyName+" is required", validationResult) |
| 53 | +} |
| 54 | + |
| 55 | +// PropertyPatternMismatch returns whether the given property did not match the regular expression defined in the JSON schema. |
| 56 | +func PropertyPatternMismatch(propertyName string, validationResult *gojsonschema.Result) bool { |
| 57 | + return ValidationErrorMatch("pattern", propertyName, "", validationResult) |
| 58 | +} |
| 59 | + |
| 60 | +// ValidationErrorMatch returns whether the given query matches against the JSON schema validation error. |
| 61 | +// See: https://github.com/xeipuuv/gojsonschema#working-with-errors |
| 62 | +func ValidationErrorMatch(typeQuery string, fieldQuery string, descriptionQueryRegexp string, validationResult *gojsonschema.Result) bool { |
| 63 | + if validationResult.Valid() { |
| 64 | + // No error, so nothing to match |
| 65 | + return false |
| 66 | + } |
| 67 | + for _, validationError := range validationResult.Errors() { |
| 68 | + if typeQuery == "" || typeQuery == validationError.Type() { |
| 69 | + if fieldQuery == "" || fieldQuery == validationError.Field() { |
| 70 | + descriptionQuery := regexp.MustCompile(descriptionQueryRegexp) |
| 71 | + return descriptionQuery.MatchString(validationError.Description()) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return false |
| 77 | +} |
0 commit comments