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

docs: add number related documentations #331

Merged
merged 6 commits into from
Dec 30, 2023
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
98 changes: 97 additions & 1 deletion website/src/routes/api/(schemas)/number/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,104 @@ title: number
source: /schemas/number/number.ts
contributors:
- fabian-hiller
- kazizi55
---

import { ApiList, Property } from '~/components';
import { properties } from './properties';

# number

> The content of this page is not yet ready. Until then, please use the [source code](https://github.com/fabian-hiller/valibot/blob/main/library/src/schemas/number/number.ts) or take a look at [issue #287](https://github.com/fabian-hiller/valibot/issues/287) to help us extend the API reference.
Creates a number schema.

```ts
// Number schema with an optional pipe
const Schema = number(pipe);

// Number schema with an optional message and pipe
const Schema = number(message, pipe);
```

## Parameters

- `message` <Property {...properties.message} />
- `pipe` <Property {...properties.pipe}/>

### Explanation

With `number` you can validate the data type of the input and with `pipe` you can transform and validate the further details of the number. If the input is not an number, you can use `message` to customize the error message.

## Returns

- `Schema` <Property {...properties.Schema} />

## Examples

The following examples show how `number` can be used.

### Integer schema

Schema to validate an integer.

```ts
const IntegerSchema = number([integer()]);
```

### Force minimum

Schema that forces a minimum number of 10.

```ts
const MinNumberSchema = number([toMinValue(10)]);
```

### Validate range

Schema that validates a number in a range.

```ts
const NumberRangeSchema = number([minValue(1), maxValue(100)]);
```

## Related

The following APIs can be combined with `number`.

### Methods

<ApiList
items={[
'brand',
'coerce',
'fallback',
'getDefault',
'getDefaults',
'getFallback',
'getFallbacks',
'is',
'parse',
'safeParse',
'transform',
'withDefault',
]}
/>

### Transformations

<ApiList items={['toCustom', 'toMaxValue', 'toMinValue']} />

### Validations

<ApiList
items={[
'custom',
'finite',
'integer',
'maxValue',
'minValue',
'multipleOf',
'notValue',
'safeInteger',
'value',
]}
/>
38 changes: 38 additions & 0 deletions website/src/routes/api/(schemas)/number/properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { PropertyProps } from '~/components';

export const properties: Record<string, PropertyProps> = {
message: {
type: [
{
type: 'custom',
name: 'ErrorMessage',
href: '../ErrorMessage/',
},
'undefined',
],
default: {
type: 'string',
value: 'Invalid type',
},
},
pipe: {
type: [
{
type: 'custom',
name: 'Pipe',
href: '../Pipe/',
generics: ['number'],
},
'undefined',
],
},
Schema: {
type: [
{
type: 'custom',
name: 'NumberSchema',
href: '../NumberSchema/',
},
],
},
};
21 changes: 21 additions & 0 deletions website/src/routes/api/(types)/NumberSchema/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: NumberSchema
description: Number schema type.
contributors:
- fabian-hiller
- kazizi55
---

import { Property } from '~/components';
import { properties } from './properties';

# NumberSchema

Number schema type.

## Definition

- `NumberSchema` <Property {...properties.BaseSchema} />
- `type` <Property {...properties.type} />
- `message` <Property {...properties.message} />
- `pipe` <Property {...properties.pipe} />
45 changes: 45 additions & 0 deletions website/src/routes/api/(types)/NumberSchema/properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { PropertyProps } from '~/components';

export const properties: Record<string, PropertyProps> = {
BaseSchema: {
type: [
{
type: 'custom',
name: 'BaseSchema',
href: '../BaseSchema/',
generics: [
'number',
{
type: 'custom',
name: 'TOutput',
default: 'number',
},
],
},
],
},
type: {
type: {
type: 'string',
value: 'number',
},
},
message: {
type: {
type: 'custom',
name: 'ErrorMessage',
href: '../ErrorMessage/',
},
},
pipe: {
type: [
{
type: 'custom',
name: 'Pipe',
href: '../Pipe/',
generics: ['number'],
},
'undefined',
],
},
};
1 change: 1 addition & 0 deletions website/src/routes/api/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
- [MaybeReadonly](/api/MaybeReadonly/)
- [NullableSchema](/api/NullableSchema/)
- [NullishSchema](/api/NullishSchema/)
- [NumberSchema](/api/NumberSchema/)
- [ObjectPathItem](/api/ObjectPathItem/)
- [OptionalSchema](/api/OptionalSchema/)
- [Output](/api/Output/)
Expand Down