Stof Docs
Star on GitHubStof WebsiteDiscordContact Us
  • 🚀Welcome
  • â„šī¸Resources & Information
  • Book
    • Data Interface
    • Introduction & Design
    • Host Environments
      • Rust
      • TypeScript
        • Extend Stof
        • Import Statement
    • Data Interchange
      • JSON to Stof
      • Importing and Parsing Data
      • Exporting Data
      • REST and Restructure
    • Configuration Files
    • Schemas
      • Renaming Fields
      • Removing Fields
      • Validation & Transformation
        • Example Access
      • Nested Schemas
    • Orchestration
  • Common Concepts
    • Objects
    • Primitive Types
    • Functions
    • Fields
    • Object Types
    • Imports
    • Error Handling
    • Units
    • Casting/Conversions
  • Reference
    • CLI
      • 🏃Run
      • đŸ§ĒTest
      • 📚CLI Libraries
        • Filesystem Library
        • Time Library
        • HTTP Library
    • Stof Language
    • Libraries
      • Standard Library
      • Array/Vector Library
      • Number Library
      • String Library
      • Object Library
      • Function Library
      • Set Library
      • Map Library
      • Data Library
      • Tuple Library
      • Blob Library
      • Boolean Library
    • đŸĒ§Formats
Powered by GitBook
On this page
  • Remove Invalid Fields
  • Remove Non-Schema Fields
  • Conditionally Remove Fields

Was this helpful?

  1. Book
  2. Schemas

Removing Fields

Removing fields with schemafy.

PreviousRenaming FieldsNextValidation & Transformation

Last updated 3 months ago

Was this helpful?

The schemafy function in the gives two parameters for removing fields: one for removing invalid fields (default behavior) and one for removing any fields on a target object that are not defined on the schema object (not enabled by default).

However, one can always use the "removeField" function in the Object Library as well. Maybe for conditional removal or more involved use cases.

Remove Invalid Fields

Anytime a schema function has a boolean as the return type in the function signature, schemafy will interpret this to be a validation check instead of a new value to set. The default behavior is to remove values that return false (invalid).

You can return boolean values as a new, valid value (instead of a valid check) by setting the function signature return type to "unknown" instead of "bool". It is based on the type signature, not the returned value type.

schema: {
    #[schema((value: int): bool => value >= 0 && value <= 200)]
    field: 0
}

target: {
    field: -42
}

#[main]
fn main() {
    assertNot(self.schema.schemafy(self.target));
    pln(self.target);
}
> stof run example.stof
{}

To disable this functionality:

schema: {
    #[schema((value: int): bool => value >= 0 && value <= 200)]
    field: 0
}

target: {
    field: -42
}

#[main]
fn main() {
    assertNot(self.schema.schemafy(self.target, false)); // still not valid though
    pln(self.target);
}
> stof run example.stof
{"field":-42}

Remove Non-Schema Fields

schema: {
    #[schema((value: int): bool => value >= 0 && value <= 200)]
    field: 0
}

target: {
    field: 42
    
    // additional fields we don't care about
    a: "a"
    b: "b"
    c: "c"
    d: {
        e: "e"
        f: "f"
    }
}

#[main]
fn main() {
    assert(self.schema.schemafy(self.target, true, true));
    pln(self.target);
}
> stof run example.stof
{"field":42}

Conditionally Remove Fields

schema: {
    #[schema((target: obj, value: int): bool => {
        drop target.another_field; // or target.removeField("another_field")
        if (value < 20) return false;
        return true;
    })]
    field: 0
}

target: {
    field: 42  // only removed if value is less than 20
    another_field: "will be removed"
}

#[main]
fn main() {
    assert(self.schema.schemafy(self.target));
    pln(self.target);
}
> stof run example.stof
{"field":42}
Object Library