Debian Codegraph @

errors/xml_errors.go

raw
1 package errors
2
3 import (
4 "fmt"
5
6 "github.com/pb33f/libopenapi-validator/helpers"
7 "github.com/pb33f/libopenapi/datamodel/high/base"
8 )
9
10 func MissingPrefix(schema *base.Schema, prefix string) *ValidationError {
11 line := 1
12 col := 0
13 if low := schema.GoLow(); low != nil && low.Type.KeyNode != nil {
14 line = low.Type.KeyNode.Line
15 col = low.Type.KeyNode.Column
16 }
17
18 return &ValidationError{
19 ValidationType: helpers.XmlValidation,
20 ValidationSubType: helpers.XmlValidationPrefix,
21 Message: fmt.Sprintf("The prefix '%s' is defined in the schema, however it's missing from the xml", prefix),
22 Reason: fmt.Sprintf("The prefix '%s' is defined in the schema, however it's missing from the xml content", prefix),
23 SpecLine: line,
24 SpecCol: col,
25 Context: schema,
26 HowToFix: fmt.Sprintf(HowToFixXmlPrefix, prefix),
27 }
28 }
29
30 func InvalidPrefix(schema *base.Schema, prefix string) *ValidationError {
31 line := 1
32 col := 0
33 if low := schema.GoLow(); low != nil && low.Type.KeyNode != nil {
34 line = low.Type.KeyNode.Line
35 col = low.Type.KeyNode.Column
36 }
37
38 return &ValidationError{
39 ValidationType: helpers.XmlValidation,
40 ValidationSubType: helpers.XmlValidationPrefix,
41 Message: fmt.Sprintf("The prefix '%s' defined in the schema differs from the xml", prefix),
42 Reason: fmt.Sprintf("The prefix '%s' is defined in the schema, however the xml sent and invalid prefix", prefix),
43 SpecCol: col,
44 SpecLine: line,
45 Context: schema,
46 HowToFix: fmt.Sprintf(HowToFixXmlPrefix, prefix),
47 }
48 }
49
50 func MissingNamespace(schema *base.Schema, namespace string) *ValidationError {
51 line := 1
52 col := 0
53 if low := schema.GoLow(); low != nil && low.Type.KeyNode != nil {
54 line = low.Type.KeyNode.Line
55 col = low.Type.KeyNode.Column
56 }
57
58 return &ValidationError{
59 ValidationType: helpers.XmlValidation,
60 ValidationSubType: helpers.XmlValidationNamespace,
61 Message: fmt.Sprintf("The namespace '%s' is defined in the schema, however it's missing from the xml", namespace),
62 Reason: fmt.Sprintf("The namespace '%s' is defined in the schema, however it's missing from the xml content", namespace),
63 SpecLine: line,
64 SpecCol: col,
65 Context: schema,
66 HowToFix: fmt.Sprintf(HowToFixXmlNamespace, namespace),
67 }
68 }
69
70 func InvalidNamespace(schema *base.Schema, namespace, expectedNamespace, prefix string) *ValidationError {
71 line := 1
72 col := 0
73 if low := schema.GoLow(); low != nil && low.Type.KeyNode != nil {
74 line = low.Type.KeyNode.Line
75 col = low.Type.KeyNode.Column
76 }
77
78 return &ValidationError{
79 ValidationType: helpers.XmlValidation,
80 ValidationSubType: helpers.XmlValidationNamespace,
81 Message: fmt.Sprintf("The namespace from prefix '%s' differs from the xml", prefix),
82 Reason: fmt.Sprintf("The namespace from prefix '%s' is declared as '%s' in the schema, however in xml is declared as '%s'",
83 prefix, expectedNamespace, namespace),
84 SpecLine: line,
85 SpecCol: col,
86 Context: schema,
87 HowToFix: fmt.Sprintf(HowToFixXmlNamespace, namespace),
88 }
89 }
90
91 func InvalidXMLParsing(reason, referenceObject string) *ValidationError {
92 return &ValidationError{
93 ValidationType: helpers.XmlValidation,
94 ValidationSubType: helpers.Schema,
95 Message: "xml example is malformed",
96 Reason: fmt.Sprintf("failed to parse xml: %s", reason),
97 SchemaValidationErrors: []*SchemaValidationFailure{{
98 Reason: reason,
99 ReferenceSchema: "",
100 ReferenceObject: referenceObject,
101 }},
102 HowToFix: HowToFixInvalidXml,
103 }
104 }