*nutype* is a procedural macro that adds sanitization and validation to newtypes, ensuring that values always meet your defined constraints.
## What’s new in v0.6.2
* *\[FEATURE]* Added `derive_unsafe(..)` for deriving arbitrary traits (requires enabling the `derive_unsafe` feature).
* *\[FIX]* Upgraded Rust edition: *2021 → 2024*.
* *\[FIX]* Improved error messages for `len_char_max` and `len_char_min`—they’re now clearer and easier to interpret.
## About `derive_unsafe`
With `derive_unsafe(..)`, you can now derive *any trait*, including third-party ones that nutype doesn’t recognize.
Unlike the safe `derive(..)`, this skips nutype’s internal checks. That means *you can accidentally break your validations at runtime*—for example, by enabling mutation via `DerefMut`.
To use this feature, add the `derive_unsafe` flag.
*Cautionary example:*
```rust
use derive_more::{Deref, DerefMut};
use nutype::nutype;
Nutype is a proc macro that allows adding extra constraints like sanitization and validation to the regular newtype pattern. The generated code makes it impossible to instantiate a value without passing the checks. It works this way even with serde deserialization.
There still "weak" points, where incorrect typing can be introduced, e.g. incorrectly written bindings.
Regarding data from HTTP call in theory they can be cast to any type abusing `external` keyword which is meant for binings.
But common and recommend approach is to write codes. Codec would perform all the necessary checks to ensure that the data are in correct shape to satisfy the type restrictions.
You can see some examples here: https://github.com/greyblake/from-typescript-to-rescript/blo...
I agree, ReScript has no big adoption (hopefully yet), and that's the biggest disadvantage in comparison with TypeScript.
But somebody has to do the first steps...
There's some project to get TS type annotations to work for ReScript. It'd be great if ReScript could lift on the work done by the TS community to produce types for many common JS libs.
I wonder how would that work. Typescript has pretty advanced types like Template Literal Types[0]. Wouldn't ReScript need to support all those types? Or maybe it just resorts to `any` equivalent if I can't map a TS type to ReScript type.
Yeah I also noticed Purescript was missing from your comparisons list. It's the only one of these that I've looked at much, and it seemed like the best of them, though I wasn't aware of the issue with ES6 modules. I don't use JS at all for now, so I haven't been up to date about anything in it.
*nutype* is a procedural macro that adds sanitization and validation to newtypes, ensuring that values always meet your defined constraints.
## What’s new in v0.6.2
* *\[FEATURE]* Added `derive_unsafe(..)` for deriving arbitrary traits (requires enabling the `derive_unsafe` feature). * *\[FIX]* Upgraded Rust edition: *2021 → 2024*. * *\[FIX]* Improved error messages for `len_char_max` and `len_char_min`—they’re now clearer and easier to interpret.
## About `derive_unsafe`
With `derive_unsafe(..)`, you can now derive *any trait*, including third-party ones that nutype doesn’t recognize.
Unlike the safe `derive(..)`, this skips nutype’s internal checks. That means *you can accidentally break your validations at runtime*—for example, by enabling mutation via `DerefMut`.
To use this feature, add the `derive_unsafe` flag.
*Cautionary example:*
```rust use derive_more::{Deref, DerefMut}; use nutype::nutype;
#[nutype( derive(Debug, AsRef), derive_unsafe(Deref, DerefMut), validate(greater_or_equal = 0.0, less_or_equal = 2.0) )] struct LlmTemperature(f64);
fn main() { let mut temperature = LlmTemperature::try_new(1.5).unwrap();
} ```*TL;DR:* `derive_unsafe` gives you freedom—but with that comes full responsibility. Use wisely.