amazon-ion / ion-schema-rust

Rust implementation of Ion Schema
https://amazon-ion.github.io/ion-schema/sandbox
Apache License 2.0
12 stars 6 forks source link

Modifies violation messages to include ISL struct for annoymous type definitions #142

Closed desaikd closed 1 year ago

desaikd commented 1 year ago

Issue #102:

Description of changes:

This PR works on adding ISL struct in the violation messages for anonymous type definitions. This adds more context in the violation message regarding where in the type definition the violation occurred.

List of changes:

Example of a violation message: Schema:

type::{
  name: one_of_type,
  one_of: [ { type: string, codepoint_length: range::[1, 10] }, decimal ],
}

Type to be validated:

one_of_type

Ion value used for validation:

1

New violation message changed with this PR:

Violation {
    constraint: "one_of_type",
    code: TypeConstraintsUnsatisfied,
    message: "value didn't satisfy type constraint(s)",
    violations: [
        Violation {
            constraint: "one_of",
            code: NoTypesMatched,
            message: "value matches none of the types",
            violations: [
                Violation {
                    constraint: "{type: string, codepoint_length: range::[1, 10]}",  // <--- gives a more precise location information
                    code: TypeConstraintsUnsatisfied,
                    message: "value didn't satisfy type constraint(s)",
                    violations: [
                        Violation {
                            constraint: "type_constraint",
                            code: TypeMismatched,
                            message: "expected type String, found Integer",
                            violations: [],
                        },
                        Violation {
                            constraint: "codepoint_length",
                            code: TypeMismatched,
                            message: "expected [String, Symbol] but found integer",
                            violations: [],
                        },
                    ],
                },
                Violation {
                    constraint: "type_constraint",
                    code: TypeMismatched,
                    message: "expected type Decimal, found Integer",
                    violations: [],
                },
            ],
        },
    ],
}

Previous violation message (prior tot his PR):

Violation {
    constraint: "one_of_type",
    code: TypeConstraintsUnsatisfied,
    message: "value didn't satisfy type constraint(s)",
    violations: [
        Violation {
            constraint: "one_of",
            code: NoTypesMatched,
            message: "value matches none of the types",
            violations: [
                Violation {
                    constraint: "",
                    code: TypeConstraintsUnsatisfied,
                    message: "value didn't satisfy type constraint(s)",
                    violations: [
                        Violation {
                            constraint: "type_constraint",
                            code: TypeMismatched,
                            message: "expected type String, found Integer",
                            violations: [],
                        },
                        Violation {
                            constraint: "codepoint_length",
                            code: TypeMismatched,
                            message: "expected [String, Symbol] but found integer",
                            violations: [],
                        },
                    ],
                },
                Violation {
                    constraint: "type_constraint",
                    code: TypeMismatched,
                    message: "expected type Decimal, found Integer",
                    violations: [],
                },
            ],
        },
    ],
}

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.