-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathvalidate.go
More file actions
51 lines (42 loc) · 1.21 KB
/
validate.go
File metadata and controls
51 lines (42 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package plugin
import (
"context"
"errors"
"fmt"
"strings"
"github.com/cloudquery/plugin-sdk/v4/schema"
"github.com/santhosh-tekuri/jsonschema/v6"
)
func validateTables(tables schema.Tables) error {
if err := tables.ValidateDuplicateTables(); err != nil {
return fmt.Errorf("found duplicate tables in plugin: %w", err)
}
if err := tables.ValidateDuplicateColumns(); err != nil {
return fmt.Errorf("found duplicate columns in plugin: %w", err)
}
return nil
}
func (p *Plugin) validate(ctx context.Context) error {
if p.skipTableValidation {
return nil
}
tables, err := p.client.Tables(ctx, TableOptions{Tables: []string{"*"}})
// ErrNotImplemented means it's a destination only plugin
if err != nil && !errors.Is(err, ErrNotImplemented) {
return fmt.Errorf("failed to get tables: %w", err)
}
return validateTables(tables)
}
func JSONSchemaValidator(jsonSchema string) (*jsonschema.Schema, error) {
c := jsonschema.NewCompiler()
c.DefaultDraft(jsonschema.Draft2020)
c.AssertFormat()
s, err := jsonschema.UnmarshalJSON(strings.NewReader(jsonSchema))
if err != nil {
return nil, err
}
if err := c.AddResource("schema.json", s); err != nil {
return nil, err
}
return c.Compile("schema.json")
}