validator

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2023 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

TODO: add dereferencing functionality

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractSchemaWithJSONPath

func ExtractSchemaWithJSONPath(spec interface{}, query string) (interface{}, error)

ExtractSchemaWithJSONPath retrieves a part of a given spec based on a JSON Path query.

Parameters:

  • spec: The data structure (usually from a parsed JSON document) from which a part needs to be extracted.
  • query: The JSON Path query string used to specify the part of the spec to extract.

Returns:

  • The extracted data based on the provided JSON Path query.
  • An error if the JSON Path query is invalid, if there's no match, or if there are other issues during extraction.

Important considerations:

  1. The function uses the 'github.com/oliveagle/jsonpath' library to perform the extraction. Familiarity with its syntax and constraints is recommended.
  2. The given spec should preferably be a map or slice. Providing simple data types might result in unexpected behaviors.
  3. If the query does not match any part of the spec, an error indicating "unknown key" will be returned.

func LoadAsyncAPISpec

func LoadAsyncAPISpec(spec string) (interface{}, error)

LoadAsyncAPISpec loads and parses an AsyncAPI specification from the given spec.

Parameters:

  • spec: the AsyncAPI specification, in JSON format.

Returns:

  • A generic interface{} representing the parsed AsyncAPI specification.
  • An error if there are issues reading the file or parsing the contained JSON.

Important considerations:

  1. The function expects the file at the provided path to contain a valid JSON representation of an AsyncAPI spec.
  2. The returned interface{} is typically a map[string]interface{} for JSON objects or a slice for JSON arrays. Type assertion might be necessary based on the structure of your AsyncAPI spec.
  3. Errors might arise from file access issues (e.g., file not found, permission issues) or JSON parsing issues (e.g., malformed JSON, unexpected data types).
  4. Ensure that the provided path is either an absolute path or relative to the current working directory of the executable.

func LoadAsyncAPISpecFromFile

func LoadAsyncAPISpecFromFile(path string) (interface{}, error)

LoadAsyncAPISpecFromFile loads and parses an AsyncAPI specification from a given file path.

Parameters:

  • path: The path to the file containing the AsyncAPI specification, in JSON format.

Returns:

  • A generic interface{} representing the parsed AsyncAPI specification.
  • An error if there are issues reading the file or parsing the contained JSON.

Important considerations:

  1. The function expects the file at the provided path to contain a valid JSON representation of an AsyncAPI spec.
  2. The returned interface{} is typically a map[string]interface{} for JSON objects or a slice for JSON arrays. Type assertion might be necessary based on the structure of your AsyncAPI spec.
  3. Errors might arise from file access issues (e.g., file not found, permission issues) or JSON parsing issues (e.g., malformed JSON, unexpected data types).
  4. Ensure that the provided path is either an absolute path or relative to the current working directory of the executable.

func ValidateJSONAgainstSchema

func ValidateJSONAgainstSchema(jsonData interface{}, schema interface{}) error

ValidateJSONAgainstSchema checks if the provided JSON data adheres to a given schema.

Parameters:

  • jsonData: The data (usually a map or slice from parsed JSON) that needs validation against the schema.
  • schema: The schema (typically in JSON format) to which jsonData should adhere.

Returns:

  • nil if the jsonData matches the schema without any issues.
  • An error detailing the mismatch or any other issues encountered during validation.

Important considerations:

  1. The function uses the 'github.com/xeipuuv/gojsonschema' library for JSON schema validation. It supports the JSON Schema Draft 4, 6, and 7 specifications.
  2. Both the jsonData and schema parameters should preferably be of type map[string]interface{} or appropriate Go types representing JSON structures.
  3. If the validation fails, the function prints each validation error to the standard output.
  4. It's recommended to handle and process the returned error appropriately in your application. If validation fails, the error will have the message "json is not valid".
  5. Ensure that the provided schema is a valid JSON schema; otherwise, the function might return unexpected errors.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL