Skip to content

feat(op2): add #[validate(fn)] for custom arg validation #1137

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

Merged
merged 12 commits into from
May 29, 2025

Conversation

littledivy
Copy link
Member

@littledivy littledivy commented May 22, 2025

Node.js APIs written in object wrap need to throw errors like ERR_INVALID_ARG_TYPE and ERR_INVALID_CONSTRUCTOR before argument de-serialization which is not directly exposed in op2. This is currently not possible.

This PR adds custom validation hooks that run before de-serializing each argument using #[validate(fn)] attribute.

fn int(
  _scope: &mut v8::HandleScope,
  value: v8::Local<v8::Value>,
) -> Result<(), JsErrorBox> {
  if value.is_int32() {
    return Ok(());
  }

  Err(JsErrorBox::type_error("Expected int"))
}

#[op2]
impl Object {
  #[fast]
  fn n(
    &self,
    #[validate(int)]
    #[smi]
    n: u32,
  ) {
    // ...
  }
}

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

LGTM

@codecov-commenter
Copy link

codecov-commenter commented May 24, 2025

Codecov Report

Attention: Patch coverage is 79.43925% with 22 lines in your changes missing coverage. Please review.

Project coverage is 80.75%. Comparing base (0c7f83e) to head (3e41cbe).
Report is 305 commits behind head on main.

Files with missing lines Patch % Lines
ops/op2/config.rs 32.00% 17 Missing ⚠️
ops/op2/signature.rs 75.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1137      +/-   ##
==========================================
- Coverage   81.43%   80.75%   -0.69%     
==========================================
  Files          97      100       +3     
  Lines       23877    28957    +5080     
==========================================
+ Hits        19445    23385    +3940     
- Misses       4432     5572    +1140     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@littledivy littledivy merged commit 3edb0b7 into denoland:main May 29, 2025
18 checks passed
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.

3 participants