Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions docs/en/v1/api/clean/castConstraint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
outline: [2, 3]
prev:
text: "Constraints"
link: "/en/v1/api/clean/constraints"
next:
text: "NewType"
link: "/en/v1/api/clean/newType"
description: "Extends the typing of a constrained value by adding compatible constraints without re-validating it."
---

# castConstraint

`castConstraint` extends the typing of an already constrained value by adding one or more compatible constraints.
It does not re-validate the value; it only adds constraint markers. TypeScript prevents invalid casts.

## Interactive example

<MonacoTSEditor
src="/examples/v1/api/clean/castConstraint/tryout.doc.ts"
majorVersion="v1"
height="628px"
/>

## Syntax

### Classic signature

```typescript
function castConstraint(
constrainedType: ConstrainedType,
constraintHandler: ConstraintHandler | ConstraintHandler[]
): ConstrainedType
```

## Parameters

- `constrainedType`: A value already carrying one or more constraints.
- `constraintHandler`: A constraint handler (or an array of handlers) to add.

## Return value

A new constrained value with the same wrapped value and the additional constraint markers.

## See also

- [`constraints`](/en/v1/api/clean/constraints)
- [`newType`](/en/v1/api/clean/newType)
22 changes: 2 additions & 20 deletions docs/en/v1/api/clean/constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ prev:
text: "Primitives"
link: "/en/v1/api/clean/primitives"
next:
text: "NewType"
link: "/en/v1/api/clean/newType"
text: "castConstraint"
link: "/en/v1/api/clean/castConstraint"
---

# Constraints
Expand Down Expand Up @@ -105,14 +105,6 @@ function is(

The unique name of the constraint (e.g. `"email"`, `"int"`, ...).

#### `checkers`

The list of `DDataParser` checkers used to validate the value.

#### `primitiveHandler`

The primitive to which the constraint applies (e.g. `C.String`, `C.Number`).

## Constraints provided by the library

The library exports a few ready-to-use constraints via `C.*`:
Expand Down Expand Up @@ -177,16 +169,6 @@ Validates a strictly positive number (>= 1).
height="240px"
/>

### `PositiveInt`

Validates a strictly positive integer (>= 1).

<MonacoTSEditor
src="/examples/v1/api/clean/constraints/positiveInt.doc.ts"
majorVersion="v1"
height="240px"
/>

### `Negative`

Validates a strictly negative number (<= -1).
Expand Down
4 changes: 0 additions & 4 deletions docs/en/v1/api/clean/entity.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ The unique name of the entity (e.g. `"User"`), used as a runtime identifier.

The properties definition (as declared in `createEntity`).

#### `mapDataParser`

The `DataParser` generated from `propertiesDefinition` (handy if you want to reuse validation/transform elsewhere). It accepts `unknown` input and produces typed entity properties.

## Get the type

To retrieve the type of the generated entity:
Expand Down
3 changes: 3 additions & 0 deletions docs/en/v1/api/clean/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Primitives let you handle base types (`String`, `Number`, `Date`, …) in busine
## [Constraints](/en/v1/api/clean/constraints)
Constraints allow adding additional rules on primitives.

## [castConstraint](/en/v1/api/clean/castConstraint)
Extends a constrained value with compatible constraints without re-validating it.

## [NewType](/en/v1/api/clean/newType)
Creates a `NewType` (brand) backed by a `DataParser`, with optional constraints.

Expand Down
12 changes: 2 additions & 10 deletions docs/en/v1/api/clean/newType.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
outline: [2, 3]
description: "A NewType is a type designed to meet business requirements, while being based on an existing primitive type (or data structure). It allows you to add constraints and specific rules, ensuring that values satisfy the conditions defined by the business."
prev:
text: "Constraints"
link: "/en/v1/api/clean/constraints"
text: "castConstraint"
link: "/en/v1/api/clean/castConstraint"
next:
text: "Entities"
link: "/en/v1/api/clean/entity"
Expand Down Expand Up @@ -108,14 +108,6 @@ function getConstraint(

The unique name of the `NewType` (e.g. `"userId"`).

#### `dataParser`

The `DataParser` used to validate the data (including checkers added by constraints).

#### `constrains`

The list of constraints applied to the `NewType`.

## See also

- [Primitives](/en/v1/api/clean/primitives/)
Expand Down
6 changes: 0 additions & 6 deletions docs/en/v1/api/clean/primitives/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ function is(
): value is Primitive<RawType>
```

### Properties

#### `dataParser`

Corresponds to the [dataParser](/en/v1/api/dataParser/) that validates the primitive.

## Operators

### [equal](/en/v1/api/clean/primitives/operators/equal)
Expand Down
28 changes: 28 additions & 0 deletions docs/examples/v1/api/clean/castConstraint/tryout.doc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { C, type ExpectType } from "@duplojs/utils";

const baseMin = C.NumberMin(5).createOrThrow(7);
const widenedMin = C.castConstraint(baseMin, C.NumberMin(3));

type CheckWidenedMin = ExpectType<
typeof widenedMin,
& C.ConstrainedType<"number-min-5", 7>
& C.ConstrainedType<"number-min-3", number>,
"strict"
>;

const baseMax = C.NumberMax(5).createOrThrow(-2);
const widenedMax = C.castConstraint(
baseMax,
[
C.NumberMax(8),
C.NumberMax(10),
],
);

type CheckWidenedMax = ExpectType<
typeof widenedMax,
& C.ConstrainedType<"number-max-5", -2>
& C.ConstrainedType<"number-max-8", number>
& C.ConstrainedType<"number-max-10", number>,
"strict"
>;
11 changes: 0 additions & 11 deletions docs/examples/v1/api/clean/constraints/positiveInt.doc.ts

This file was deleted.

2 changes: 1 addition & 1 deletion docs/examples/v1/api/clean/maybe/tryout.doc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { C, DP, type ExpectType } from "@duplojs/utils";

const UserId = C.createNewType("userId", DP.number(), C.PositiveInt);
const UserId = C.createNewType("userId", DP.number(), C.Positive);
const User = C.createEntity("User", () => ({
id: UserId,
}));
Expand Down
48 changes: 48 additions & 0 deletions docs/fr/v1/api/clean/castConstraint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
outline: [2, 3]
prev:
text: "Contraintes"
link: "/fr/v1/api/clean/constraints"
next:
text: "NewType"
link: "/fr/v1/api/clean/newType"
description: "Étend le typage d'une valeur contrainte en ajoutant des contraintes compatibles sans revalider."
---

# castConstraint

`castConstraint` étend le typage d'une valeur déjà contrainte en ajoutant une ou plusieurs contraintes compatibles.
La valeur n'est pas revalidée : seules les marques de contraintes sont ajoutées. TypeScript empêche les casts invalides.

## Exemple interactif

<MonacoTSEditor
src="/examples/v1/api/clean/castConstraint/tryout.doc.ts"
majorVersion="v1"
height="628px"
/>

## Syntaxe

### Signature classique

```typescript
function castConstraint(
constrainedType: ConstrainedType,
constraintHandler: ConstraintHandler | ConstraintHandler[]
): ConstrainedType
```

## Paramètres

- `constrainedType` : Une valeur qui porte déjà une ou plusieurs contraintes.
- `constraintHandler` : Une contrainte (ou un tableau de contraintes) à ajouter.

## Valeur de retour

Une nouvelle valeur contrainte avec la même valeur wrappée et les contraintes ajoutées.

## Voir aussi

- [`constraints`](/fr/v1/api/clean/constraints)
- [`newType`](/fr/v1/api/clean/newType)
22 changes: 2 additions & 20 deletions docs/fr/v1/api/clean/constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ prev:
text: "Primitives"
link: "/fr/v1/api/clean/primitives"
next:
text: "NewType"
link: "/fr/v1/api/clean/newType"
text: "castConstraint"
link: "/fr/v1/api/clean/castConstraint"
---

# Contraintes
Expand Down Expand Up @@ -105,14 +105,6 @@ function is(

Le nom unique de la contrainte (ex: `"email"`, `"int"`, ...).

#### `checkers`

La liste des checkers du `DDataParser` utilisés pour valider la valeur.

#### `primitiveHandler`

La primitive sur laquelle s'applique la contrainte (ex: `C.String`, `C.Number`).

## Contraintes fournies par la librairie

La librairie exporte quelques contraintes prêtes à l'emploi via `C.*` :
Expand Down Expand Up @@ -177,16 +169,6 @@ Valide un nombre strictement positif (>= 1).
height="240px"
/>

### `PositiveInt`

Valide un nombre entier strictement positif (>= 1).

<MonacoTSEditor
src="/examples/v1/api/clean/constraints/positiveInt.doc.ts"
majorVersion="v1"
height="240px"
/>

### `Negative`

Valide un nombre strictement négatif (<= -1).
Expand Down
4 changes: 0 additions & 4 deletions docs/fr/v1/api/clean/entity.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ Le nom unique de l'entité (ex: `"User"`), utilisé comme identifiant runtime.

La définition des propriétés (telle que déclarée dans `createEntity`).

#### `mapDataParser`

Le `DataParser` généré à partir de `propertiesDefinition` (pratique si vous voulez réutiliser la validation/transform ailleurs). Il accepte une entrée `unknown` et produit des propriétés d'entité typées.

## Récupérer le type

Pour récupérer le type de l'entité générée :
Expand Down
3 changes: 3 additions & 0 deletions docs/fr/v1/api/clean/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Les primitives permettent de manipuler dans du métier des types de base (`Strin
## [Contraintes](/fr/v1/api/clean/constraints)
Les contraintes permettent d'ajouter des règles supplémentaires sur les primitives.

## [castConstraint](/fr/v1/api/clean/castConstraint)
Étend une valeur contrainte avec des contraintes compatibles sans revalidation.

## [NewType](/fr/v1/api/clean/newType)
Crée un `NewType` (brand) adossé à un `DataParser`, avec contraintes optionnelles.

Expand Down
12 changes: 2 additions & 10 deletions docs/fr/v1/api/clean/newType.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
outline: [2, 3]
description: "Un NewType est un type conçu pour répondre aux exigences métier, tout en étant basé sur un type primitif (ou une structure de données) existant. Il permet d'ajouter des contraintes et des règles spécifiques, garantissant que les valeurs respectent les conditions définies par le métier."
prev:
text: "Contraintes"
link: "/fr/v1/api/clean/constraints"
text: "castConstraint"
link: "/fr/v1/api/clean/castConstraint"
next:
text: "Entités"
link: "/fr/v1/api/clean/entity"
Expand Down Expand Up @@ -108,14 +108,6 @@ function getConstraint(

Le nom unique du `NewType` (ex: `"userId"`).

#### `dataParser`

Le `DataParser` utilisé pour valider la donnée (incluant les checkers ajoutés par les contraintes).

#### `constrains`

La liste des contraintes appliquées au `NewType`.

## Voir aussi

- [Primitives](/fr/v1/api/clean/primitives/)
Expand Down
6 changes: 0 additions & 6 deletions docs/fr/v1/api/clean/primitives/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ function is(
): value is Primitive<RawType>
```

### Propriétés

#### `dataParser`

Correspond au [dataParser](/fr/v1/api/dataParser/) qui valide la primitive.

## Opérateurs

### [equal](/fr/v1/api/clean/primitives/operators/equal)
Expand Down
2 changes: 1 addition & 1 deletion docs/public/libs/v1/array/types/createTuple.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { type IsEqual } from "../../common/types/isEqual";
export type CreateTuple<GenericValue extends unknown, GenericLength extends number, GenericLastTuple extends unknown[] = []> = IsEqual<GenericLength, number> extends true ? GenericValue[] : IsEqual<GenericLength, 0> extends true ? [] : [...GenericLastTuple, GenericValue] extends infer InferredResult extends any[] ? IsEqual<InferredResult["length"], GenericLength> extends true ? InferredResult : IsEqual<InferredResult["length"], 1000> extends true ? [...InferredResult, ...GenericValue[]] : CreateTuple<GenericValue, GenericLength, InferredResult> : never;
export type CreateTuple<GenericValue extends unknown, GenericLength extends number, GenericLastTuple extends readonly unknown[] = []> = IsEqual<GenericLength, number> extends true ? GenericValue[] : IsEqual<GenericLength, 0> extends true ? [] : [...GenericLastTuple, GenericValue] extends infer InferredResult extends any[] ? IsEqual<InferredResult["length"], GenericLength> extends true ? InferredResult : IsEqual<InferredResult["length"], 1000> extends true ? [...InferredResult, ...GenericValue[]] : CreateTuple<GenericValue, GenericLength, InferredResult> : never;
7 changes: 7 additions & 0 deletions docs/public/libs/v1/clean/constraint/base.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function createConstraint(name, primitiveHandler, checker) {
const dataParserWithCheckers = primitiveHandler
.dataParser
.addChecker(...checkers);
const constraintKindValue = { [name]: null };
function create$2(data) {
const result = dataParserWithCheckers.parse(unwrap.unwrap(data));
if (is.isLeft(result)) {
Expand Down Expand Up @@ -68,6 +69,12 @@ function createConstraint(name, primitiveHandler, checker) {
name,
primitiveHandler,
checkers,
internal: {
primitiveHandler,
dataParser: dataParserWithCheckers,
checkers,
constraintKindValue,
},
create: create$2,
createOrThrow,
createWithUnknown: create$2,
Expand Down
Loading
Loading