pub struct ObjectSchema {
pub fields: Vec<FieldDef>,
}Expand description
Schema for an object with known fields.
Field names are fuzzy-repaired against the defined names; field
values are repaired according to each field’s FieldKind.
Because FieldKind can contain nested ObjectSchemas, repair
recurses to any depth.
§Example
use fuzzy_parser::{FieldKind, ObjectSchema};
let schema = ObjectSchema::new(["name"])
.with_field_kind("timeout", FieldKind::Integer)
.with_field_kind("derives", FieldKind::enum_array(["Debug", "Clone"]))
.with_field_kind(
"inner",
FieldKind::Object(ObjectSchema::new(["host", "port"])),
);Fields§
§fields: Vec<FieldDef>The defined fields.
Implementations§
Source§impl ObjectSchema
impl ObjectSchema
Sourcepub fn from_json_schema(
root: &Value,
) -> Result<SchemaImport<ObjectSchema>, FuzzyError>
pub fn from_json_schema( root: &Value, ) -> Result<SchemaImport<ObjectSchema>, FuzzyError>
Convert a JSON Schema document for a plain (non-enum) object into
an ObjectSchema.
§Example
use fuzzy_parser::{FieldKind, ObjectSchema};
use serde_json::json;
let json_schema = json!({
"type": "object",
"properties": {
"name": {"type": "string"},
"timeout": {"type": "integer"}
}
});
let import = ObjectSchema::from_json_schema(&json_schema).unwrap();
assert_eq!(import.schema.kind_of("timeout"), Some(&FieldKind::Integer));Source§impl ObjectSchema
impl ObjectSchema
Sourcepub fn new<I, S>(valid_fields: I) -> Self
pub fn new<I, S>(valid_fields: I) -> Self
Create a schema from field names (all fields accept any value shape).
Sourcepub fn with_field(self, name: impl AsRef<str>) -> Self
pub fn with_field(self, name: impl AsRef<str>) -> Self
Add a field that accepts any value shape.
Sourcepub fn with_field_kind(self, name: impl AsRef<str>, kind: FieldKind) -> Self
pub fn with_field_kind(self, name: impl AsRef<str>, kind: FieldKind) -> Self
Add a field with an expected value shape.
If a field with the same name already exists, its kind is replaced.
Sourcepub fn is_valid_field(&self, field: &str) -> bool
pub fn is_valid_field(&self, field: &str) -> bool
Check if a field name is valid.
Sourcepub fn field_names(&self) -> impl Iterator<Item = &str>
pub fn field_names(&self) -> impl Iterator<Item = &str>
Iterate over the defined field names.
Trait Implementations§
Source§impl Clone for ObjectSchema
impl Clone for ObjectSchema
Source§fn clone(&self) -> ObjectSchema
fn clone(&self) -> ObjectSchema
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ObjectSchema
impl Debug for ObjectSchema
Source§impl Default for ObjectSchema
impl Default for ObjectSchema
Source§fn default() -> ObjectSchema
fn default() -> ObjectSchema
Returns the “default value” for a type. Read more
Source§impl PartialEq for ObjectSchema
impl PartialEq for ObjectSchema
impl StructuralPartialEq for ObjectSchema
Auto Trait Implementations§
impl Freeze for ObjectSchema
impl RefUnwindSafe for ObjectSchema
impl Send for ObjectSchema
impl Sync for ObjectSchema
impl Unpin for ObjectSchema
impl UnsafeUnpin for ObjectSchema
impl UnwindSafe for ObjectSchema
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more