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 array related documentations #330

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
112 changes: 111 additions & 1 deletion website/src/routes/api/(schemas)/array/index.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,120 @@
---
title: array
description: Creates an array schema.
source: /schemas/array/array.ts
contributors:
- fabian-hiller
- kazizi55
---

import { Link } from '@builder.io/qwik-city';
import { ApiList, Property } from '~/components';
import { properties } from './properties';

# array

> 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/array/array.ts) or take a look at [issue #287](https://github.com/fabian-hiller/valibot/issues/287) to help us extend the API reference.
Creates an array schema.

```ts
// Array schema with an optional pipe
const Schema = array<TItem>(item, pipe);

// Array schema with an optional message and pipe
const Schema = array<TItem>(item, message, pipe);
```

## Generics

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

## Parameters

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

### Explanation

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

> If your array has a fixed length, consider using <Link href="../tuple/">`tuple`</Link> for a more precise typing.

## Returns

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

## Examples

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

### String array schema

Schema to validate an array of strings.

```ts
const StringArraySchema = array(string(), 'An array is required.');
```

### Object array schema

Schema to validate an array of objects.

```ts
const ObjectArraySchema = array(object({ key: string() }));
```

### Validate length

Schema that validates the length of an array.

```ts
const ArrayLengthSchema = array(number(), [minLength(1), maxLength(3)]);
```

### Validate content

Schema that validates the content of an array.

```ts
const ArrayContentSchema = array(string(), [includes('foo'), excludes('bar')]);
```

## Related

The following APIs can be combined with `array`.

### Methods

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

### Transformations

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

### Validations

<ApiList
items={[
'custom',
'excludes',
'includes',
'length',
'maxLength',
'minLength',
'notLength',
]}
/>
68 changes: 68 additions & 0 deletions website/src/routes/api/(schemas)/array/properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { PropertyProps } from '~/components';

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

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

# ArraySchema

Array schema type.

## Definition

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

export const properties: Record<string, PropertyProps> = {
BaseSchema: {
type: [
{
type: 'custom',
name: 'BaseSchema',
href: '../BaseSchema/',
generics: [
{
type: 'array',
item: {
type: 'custom',
name: 'Input',
href: '../Input/',
generics: [
{
type: 'custom',
name: 'TItem',
},
],
},
},
{
type: 'custom',
name: 'TOutput',
default: {
type: 'array',
item: {
type: 'custom',
name: 'Output',
href: '../Output/',
generics: [
{
type: 'custom',
name: 'TItem',
},
],
},
},
},
],
},
],
},
type: {
type: {
type: 'string',
value: 'array',
},
},
item: {
type: [
{
type: 'custom',
name: 'TItem',
},
],
},
message: {
type: {
type: 'custom',
name: 'ErrorMessage',
href: '../ErrorMessage/',
},
},
pipe: {
type: [
{
type: 'custom',
name: 'Pipe',
href: '../Pipe/',
generics: [
{
type: 'array',
item: {
type: 'custom',
name: 'Output',
generics: [
{
type: 'custom',
name: 'TItem',
},
],
},
},
],
},
'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 @@ -191,6 +191,7 @@

- [AnySchema](/api/AnySchema/)
- [ArrayPathItem](/api/ArrayPathItem/)
- [ArraySchema](/api/ArraySchema/)
- [BaseSchema](/api/BaseSchema/)
- [BaseTransformation](/api/BaseTransformation/)
- [BaseValidation](/api/BaseValidation/)
Expand Down