kit

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2022 License: Apache-2.0 Imports: 6 Imported by: 3

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error interface {
	Filename() string
	Position() uint
	Message() string
	ErrCode() int
	IncorrectUserType() string
}

func LengthOfJson

func LengthOfJson(f *fs.File) (uint, Error)

LengthOfJson computes length of JSON document specified in this file. Deprecated: Use Len method of jschema.Schema instead.

Example
// non-space characters after the top level value are allowed
b := []byte(`{ "key": 123 } some extra text`)

length, err := LengthOfJson(fs.NewFile("json", b))

if err != nil {
	fmt.Println("Filename:", err.Filename()) // return "json"
	fmt.Println("Position:", err.Position())
	fmt.Println("Message:", err.Message())
	return
}

fmt.Println(length)
Output:

14

func LengthOfSchema

func LengthOfSchema(f *fs.File) (uint, Error)

LengthOfSchema computes length of schema specified in th file. Deprecated: Use Len method of jschema.Schema instead.

Example
// non-space characters after the top level value are allowed
b := []byte(`
{
	"key": 123 // {min: 1} 
}
some extra text
`)

length, err := LengthOfSchema(fs.NewFile("schema", b))

if err != nil {
	fmt.Println("Filename:", err.Filename()) // return "schema"
	fmt.Println("Position:", err.Position())
	fmt.Println("Message:", err.Message())
	return
}

fmt.Println(length)
Output:

29

func ValidateJson

func ValidateJson(
	schemaFile *fs.File,
	extraTypes map[string]*fs.File,
	jsonFile *fs.File,
	areKeysOptionalByDefault bool,
) Error

ValidateJson the key of extraTypes parameter is the name of the type. The file name is used only for display in case of an error. They may not be the same. Deprecated: Use Validate method of jschema.Schema instead.

Example
schem := []byte(` {
		"aaa": 111 // {type: "@int"}
	} `)
json := []byte(` {"aaa":333} `)

schemaFile := fs.NewFile("schema", schem)
jsonFile := fs.NewFile("json", json)

// The key of extraTypes parameter is the name of the type.
// The file name is used only for display in case of an error.
// They may not be the same.
extraTypes := make(map[string]*fs.File)
extraTypes["@int"] = fs.NewFile("@int", []byte(`222 // {min: 0}`))

err := ValidateJson(schemaFile, extraTypes, jsonFile, false)

if err != nil {
	fmt.Println("Filename:", err.Filename())
	fmt.Println("Position:", err.Position())
	fmt.Println("Message:", err.Message())
	return
}

fmt.Println("wonderful json")
Output:

wonderful json

Jump to

Keyboard shortcuts

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