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

Was this helpful?

  1. Book
  2. Schemas

Renaming Fields

Rename a field using schemafy.

PreviousSchemasNextRemoving Fields

Last updated 4 months ago

Was this helpful?

If you are aware of the specific field you'd like to rename, "schemafy" offers an interesting and easy way by asking for it in the function as a Box<str> type.

schema: {
    #[schema((field: Box<str>) => { field = "desired"; })]
    undesired_name: ""
}

target: {
    undesired_name: "this is cool"
}

#[main]
fn main() {
    self.schema.schemafy(self.target);
    pln(self.target);
}
> stof run example.stof
{"desired":"this is cool"}

However, more generally, you can always ask for the target (and schema) objects, where you have the full capabilities of Stof at your disposal. This enables you to create additional fields, drop fields, rename fields, search for fields, etc.

It is easier to read, understand, maintain, and use schemas if you always define the desired fields instead of the undesired fields (unlike in the example above). This is more of a preference and might lead to more code in cases, but it's more predictable.

This code example uses the "search" functionality in the to create a field in a specific location if it does not already exist with the name we are expecting.

schema: {
    #[schema((target: obj, value: unknown): unknown => {
        if (value == null) {
            let search = target.searchDown("othername");
            if (search) return search[0];
        }
        return value;
    })]
    desired: ""
}

target: {
    subobj: {
        another: {
            othername: "yo dude"
        }
    }
}

#[main]
fn main() {
    // remove invalid, remove undefined
    self.schema.schemafy(self.target, true, true);
    pln(self.target);
}
> stof run example.stof
{"desired":"yo dude"}
Object Library