-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add v1.1.0-next.10 documentation
- Loading branch information
1 parent
e2d8e0e
commit ffef81b
Showing
14 changed files
with
512 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
{ | ||
"index": "Introduction", | ||
"getting-started": "Getting Started" | ||
"getting-started": "Getting Started", | ||
"config": "Config", | ||
"schema": "Schema" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Config | ||
|
||
## Constructor | ||
|
||
Creates a new configuration instance based on the provided schema. | ||
|
||
#### Parameters | ||
|
||
`schema`: A schema object that defines the configuration schema using the provided methods. | ||
|
||
#### Example | ||
|
||
```ts | ||
const config = new Config({ | ||
port: Config.string(), | ||
nested: Config.object({ | ||
foo: Config.string(), | ||
}), | ||
}) | ||
``` | ||
|
||
## parse | ||
|
||
Parses and validate the configuration against the schema. | ||
|
||
#### Parameters | ||
|
||
`config`: An object containing the configuration values to be parsed. | ||
|
||
#### Example | ||
|
||
```typescript | ||
config.parse({ port: 3000, nested: { foo: 'bar' } }) | ||
``` | ||
|
||
## get | ||
Retrieves the value of a specific configuration key. | ||
|
||
#### Parameters | ||
|
||
`path`: The configuration key to retrieve. | ||
|
||
#### Example | ||
|
||
```typescript | ||
config.get('port') // 3000 | ||
config.get('nested.foo') // bar | ||
``` | ||
|
||
## getAll | ||
|
||
Returns an object containing all configuration values. | ||
|
||
#### Example | ||
|
||
```typescript | ||
config.getAll('port') // { port: 3000, nested: { foo: 'bar' } } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Schema | ||
|
||
The schema is the core structure used to define the shape and rules of the configuration. | ||
|
||
## Basic Usage | ||
|
||
```typescript | ||
import { Config } from '@fullstacksjs/config'; | ||
|
||
const mySchema = { | ||
number: Config.number(), | ||
string: Config.string(), | ||
boolean: Config.boolean(), | ||
array: Config.array(Config.string()), | ||
object: Config.object({ | ||
key: Config.string(), | ||
}), | ||
}; | ||
``` | ||
|
||
## Schema Options | ||
|
||
Base option for schema definition. | ||
|
||
### Type Definition | ||
|
||
```typescript | ||
type SchemaOptions<T> = { | ||
coerce: boolean | ||
default: T | ||
} | ||
``` | ||
### Options | ||
#### coerce | ||
Controls runtime type coercion. when enabled parser tries to coerce the provided value. (default: `true`) | ||
##### Example | ||
```typescript | ||
const config = new Config({ | ||
port: Config.number({ coerce: true }), | ||
}); | ||
config.parse({ port: '3000' }) | ||
config.get(port) // 3000 | ||
typeof config.get(port) // "number" | ||
``` | ||
|
||
```typescript | ||
const config = new Config({ | ||
port: Config.number({ coerce: false }), | ||
}); | ||
config.parse({ port: '3000' }) // Invalid configuration: The "port" expected to be "number" but a "string" was provided | ||
``` | ||
|
||
#### default | ||
|
||
Defines the default value when the value is `null` or `undefined`. | ||
|
||
```typescript | ||
const config = new Config({ | ||
port: Config.number({ default: 3000 }), | ||
}); | ||
config.parse({}) | ||
config.get(port) // 3000 | ||
|
||
config.parse({ port: undefined }) | ||
config.get(port) // 3000 | ||
|
||
config.parse({ port: null }) | ||
config.get(port) // 3000 | ||
``` | ||
|
||
## Schemas | ||
|
||
import meta from './schema/_meta.json'; | ||
import { TOC } from '../components/TOC.tsx'; | ||
|
||
<TOC base="/schema" meta={meta} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"array": "Array", | ||
"boolean": "Boolean", | ||
"number": "Number", | ||
"string": "String", | ||
"object": "Object" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Array | ||
|
||
Defines an array schema. | ||
|
||
## Parameters | ||
|
||
`schema`: [Schema](/schema) | ||
|
||
### Example | ||
|
||
```typescript | ||
const arraySchema = Config.array(Config.string()); // stirng[] | ||
``` | ||
|
||
> **Caution**: | ||
> Inner schema is alwayas `required`!\ | ||
> **Caution**: | ||
> Inner schema coercion is `false` by default! | ||
## Coercion | ||
|
||
Coercion is disabled for array. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Boolean | ||
|
||
Defines a boolean schema. | ||
|
||
## Parameters | ||
|
||
`options`: [Schema Options](./schema#schema-options) | ||
|
||
### Example | ||
|
||
```typescript | ||
const booleanSchema = Config.boolean(schemaOptions); | ||
``` | ||
|
||
## Methods | ||
|
||
### required | ||
|
||
Mark schema as required. | ||
|
||
```typescript | ||
const config = new Config({ | ||
password: Config.boolean().required(), | ||
}); | ||
|
||
config.parse({ password: undefined }) // Invalid configuration: The "password" is required but the given value is "undefined" | ||
``` | ||
|
||
## Coercion | ||
When the value is string, it coerces `'0'` or `'false'` to false (case-insensitve), otherwise to passes it to the `Boolean` function. | ||
|
||
```ts | ||
const coercion = value => { | ||
const falseRegex = /(false|0)/i; | ||
return typeof value === 'string' | ||
? !falseRegex.test(value) | ||
: Boolean(value); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Number | ||
|
||
Defines a number schema. | ||
|
||
## Parameters | ||
|
||
`options`: [Schema Options](./schema#schema-options) | ||
|
||
### Example | ||
|
||
```typescript | ||
const numberSchema = Config.number(schemaOptions); | ||
``` | ||
|
||
## Methods | ||
|
||
### min | ||
|
||
Defines minimum valid value (inclusive). | ||
|
||
```typescript | ||
const config = new Config({ | ||
age: Config.number().min(18), | ||
}); | ||
config.parse({ age: 10 }) // Invalid configuration: The "age" expected to be more than or equal to "18" but "10" was provided', | ||
``` | ||
|
||
### min | ||
|
||
Defines maximum valid value (inclusive). | ||
|
||
```typescript | ||
const config = new Config({ | ||
age: Config.number().max(20), | ||
}); | ||
config.parse({ age: 22 }) // Invalid configuration: The "age" expected to be less than or equal to "20" but "22" was provided', | ||
``` | ||
|
||
### required | ||
|
||
Mark schema as required. | ||
|
||
```typescript | ||
const config = new Config({ | ||
password: Config.number().required(), | ||
}); | ||
|
||
config.parse({ password: undefined }) // Invalid configuration: The "password" is required but the given value is "undefined" | ||
``` | ||
|
||
|
||
## Coercion | ||
|
||
```ts | ||
const coercion = value => Number(value) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Object | ||
|
||
Defines an object schema. | ||
|
||
## Parameters | ||
|
||
schema: Record\<string, [Schema](./schema)> | ||
|
||
### Example | ||
|
||
```typescript | ||
const objectSchema = Config.object({ | ||
foo: Config.string(), | ||
nested: Config.object({ | ||
bar: Config.boolean().required(), | ||
}), | ||
}); | ||
|
||
/* | ||
{ | ||
foo: string | undefined, | ||
nested: { | ||
bar: boolean; | ||
} | ||
} | ||
*/ | ||
``` | ||
|
||
> **Caution**: | ||
> Inner schema coercion is `false` by default! | ||
|
||
## Coercion | ||
|
||
Coercion is disabled for object. |
Oops, something went wrong.