Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add validate attribute to InstructMacro #10

Merged
merged 2 commits into from
Jul 4, 2024

Conversation

drbh
Copy link
Contributor

@drbh drbh commented Jun 26, 2024

This PR adds a validate attribute and method to the InstructMacro macro.

validate simplifies the ability to write custom functions that "validate" each field. This can currently be done manually with serde(deserialize_with=...) however require verbose syntax.

This new macro attribute's usage can be seen in test_validation_macro or the updated getting started main.rs file

with this pr

#[derive(InstructMacro, Debug, Serialize, Deserialize)]
// This represents a single user
struct UserInfo {
    // This represents the name of the user
    #[validate(custom = "validate_uppercase")]
    name: String,
    // This represents the age of the user
    age: u8,
}

#[validate]
fn validate_uppercase(s: &String) -> Result<String, String> {
    if s.chars().any(|c| c.is_lowercase()) {
        return Err(format!(
            "Name '{}' should be entirely in uppercase. Examples: 'TIMOTHY', 'JANE SMITH'",
            s
        ));
    }
    Ok(s.to_uppercase())
}

// ...

println!("{}", result.name); // JOHN DOE
println!("{}", result.age); // 30

instead of

#[derive(InstructMacro, Debug, Serialize, Deserialize)]
// This represents a single user
struct UserInfo {
    // This represents the name of the user
    #[serde(deserialize_with = "validate_uppercase")]
    name: String,
    // This represents the age of the user
    age: u8,
}

fn validate_uppercase<'de, D>(de: D) -> Result<String, D::Error>
where
    D: Deserializer<'de>,
{
    let s = String::deserialize(de)?;
    if s.chars().any(|c| c.is_lowercase()) {
        return Err(de::Error::custom(format!(
            "Name '{}' should be entirely in uppercase. Examples: 'TIMOTHY', 'JANE SMITH'",
            s
        )));
    }
    Ok(s.to_uppercase())
}

// ...

println!("{}", result.name); // JOHN DOE
println!("{}", result.age); // 30

**note this PR includes a couple small refactors and changes the dependencies to relative paths (this is only done to ease the ability to test this PR). Please let me know if any changes should be made or if this functionality is not aligned with this library.

Thank you!!

Copy link
Collaborator

@ivanleomk ivanleomk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed up some changes, lgtm! Thanks for contributing

@ivanleomk ivanleomk merged commit f0747a3 into instructor-ai:main Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants