Skip to content

Commit

Permalink
refactor: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
santhosh-tekuri committed Apr 10, 2024
1 parent f2b2e03 commit 9d90497
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ impl<'s, 'v> Display for ErrorKind<'s, 'v> {
Self::Pattern { got, want } => {
write!(f, "{} does not match pattern {}", quote(got), quote(want))
}
Self::ContentEncoding { want, err, .. } => {
Self::ContentEncoding { want, err } => {
write!(f, "value is not {} encoded: {err}", quote(want))
}
Self::ContentMediaType { want, err, .. } => {
Expand Down
2 changes: 1 addition & 1 deletion src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Root {
format!("no root resource found for {}", self.url).into(),
))?
} else {
// look for resource with id==url
// look for resource with id==uf.url
let Some(res) = self.resources.values().find(|res| res.id == uf.url) else {
return Ok(None); // external url
};
Expand Down
32 changes: 17 additions & 15 deletions src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ struct Validator<'v, 's, 'd, 'e> {
scope: Scope<'d>,
uneval: Uneval<'v>,
errors: Vec<ValidationError<'s, 'v>>,
bool_result: bool,
bool_result: bool, // is interested to know valid or not (but not actuall error)
}

impl<'v, 's, 'd, 'e> Validator<'v, 's, 'd, 'e> {
Expand All @@ -108,6 +108,7 @@ impl<'v, 's, 'd, 'e> Validator<'v, 's, 'd, 'e> {
};
}

// check cycle --
if let Some(scp) = self.scope.check_cycle() {
let kind = ErrorKind::RefCycle {
url: &self.schema.loc,
Expand All @@ -127,17 +128,24 @@ impl<'v, 's, 'd, 'e> Validator<'v, 's, 'd, 'e> {
}
}

// constant --
if let Some(c) = &s.constant {
if !equals(v, c) {
return Err(self.error(kind!(Const, want: c)));
}
}

// enum --
if let Some(Enum { types, values }) = &s.enum_ {
if !types.contains(Type::of(v)) || !values.iter().any(|e| equals(e, v)) {
return Err(self.error(kind!(Enum, want: values)));
}
}

// constant --
if let Some(c) = &s.constant {
if !equals(v, c) {
return Err(self.error(kind!(Const, want: c)));
// format --
if let Some(format) = &s.format {
if let Err(e) = (format.func)(v) {
self.add_error(kind!(Format, Cow::Borrowed(v), format.name, e));
}
}

Expand All @@ -150,13 +158,7 @@ impl<'v, 's, 'd, 'e> Validator<'v, 's, 'd, 'e> {
self.errors.extend(result.err());
}

// format --
if let Some(format) = &s.format {
if let Err(e) = (format.func)(v) {
self.add_error(kind!(Format, Cow::Borrowed(v), format.name, e));
}
}

// type specific validations --
match v {
Value::Object(obj) => self.obj_validate(obj),
Value::Array(arr) => self.arr_validate(arr),
Expand All @@ -165,7 +167,7 @@ impl<'v, 's, 'd, 'e> Validator<'v, 's, 'd, 'e> {
_ => {}
}

if !self.bool_result || self.errors.is_empty() {
if self.errors.is_empty() || !self.bool_result {
if s.draft_version >= 2019 {
self.refs_validate();
}
Expand Down Expand Up @@ -225,9 +227,9 @@ impl<'v, 's, 'd, 'e> Validator<'v, 's, 'd, 'e> {
}

// dependencies --
for (prop, dependency) in &s.dependencies {
for (prop, dep) in &s.dependencies {
if obj.contains_key(prop) {
match dependency {
match dep {
Dependency::Props(required) => {
if let Some(missing) = self.find_missing(obj, required) {
self.add_error(ErrorKind::Dependency { prop, missing });
Expand Down

0 comments on commit 9d90497

Please sign in to comment.