Skip to content

Commit

Permalink
add more
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalbdivad committed Nov 26, 2024
1 parent 44e551f commit c40ea7c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 34 deletions.
61 changes: 59 additions & 2 deletions ark/docs/src/content/docs/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,69 @@ title: Configuration

### Clone

🚧 Coming soon ™️🚧
By default, before a [morph](/intro/morphs-and-more) is applied, ArkType will deeply clone the original input value with a built-in `deepClone` function that tries to make reasonable assumptions about preserving prototypes etc. The implementation of `deepClone` can be found [here](https://github.com/arktypeio/arktype/blob/main/ark/util/clone.ts).

You can provide an alternate clone implementation to the `clone` config option.

```ts
import { configure } from "arktype/config"

configure({ clone: structuredClone })

import { type } from "arktype"

// will now create a new object using structuredClone
const userForm = type({
age: "string.numeric.parse"
})
```

To mutate the input object directly, you can set the `clone` config option to `false`.

```ts
import { configure } from "arktype/config"

configure({ clone: false })

import { type } from "arktype"

const userForm = type({
age: "string.numeric.parse"
})

const formData = {
age: "42"
}

const out = userForm(formData)

// the original object's age key is now a number
console.log(formData.age)
```

### onUndeclaredKey

🚧 Coming soon ™️🚧

### jitless

🚧 Coming soon ™️🚧
By default, when a `Type` is instantiated, ArkType will precompile optimized validation logic that will run when the type is invoked. This behavior is disabled by default in environments that don't support `new Function`, e.g. Cloudlflare Workers.

If you'd like to opt out of it for another reason, you can set the `jitless` config option to `true`.

```ts
import { configure } from "arktype/config"

// IMPORTANT: make sure you import from the "arktype/config" entry point
// and invoke configure before importing anything else from ArkType
// unless you want the builtin types to be precompiled

configure({ jitless: true })

import { type } from "arktype"

// will not be precompiled
const myObject = type({
foo: "string"
})
```
2 changes: 1 addition & 1 deletion ark/fs/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export const readPackageJsonAncestors = (fromDir?: string): any[] =>
export const fromPackageRoot = (...joinWith: string[]): string =>
join(assertPackageRoot(dirOfCaller()), ...joinWith)

export const readPackageJson = (startDir?: string): any =>
export const readPackageJson = (startDir = dirOfCaller()): any =>
readJson(join(assertPackageRoot(startDir), "package.json"))

export const getSourceControlPaths = (): string[] =>
Expand Down
34 changes: 5 additions & 29 deletions ark/repo/scratch.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
import { type } from "arktype"

const _user = type({
name: "string | undefined"
})

interface User extends type.infer<typeof _user> {}

const user: type<User> = _user

user({}).toString()
import { configure } from "arktype/config"

Date.now()
configure({ clone: false })

const tenYearsAgo = new Date()
.setFullYear(new Date().getFullYear() - 10)
.valueOf()

const bounded = type({
dateInTheLast10Years: `${tenYearsAgo} <= Date < ${Date.now()}`
})

Date.now() //?

//?

type.Date.laterThan("")
import { type } from "arktype"

const out = bounded.assert({
dateInThePast: new Date(0),
dateAfter2000: new Date(),
dateAtOrAfter1970: new Date(0)
const userForm = type({
age: "string.numeric.parse"
})
4 changes: 3 additions & 1 deletion ark/schema/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export type ResolvedUnknownNodeConfig = requireKeys<
"description"
>

$ark.config = {}
// $ark.config could already be set if it were imported previously from the
// dedicated config entrypoint, in which case we don't want to reinitialize it
$ark.config ??= {}

export const configure = (config: ArkConfig): ArkConfig =>
Object.assign($ark.config, mergeConfigs($ark.config, config))
Expand Down
22 changes: 21 additions & 1 deletion ark/type/__tests__/clone.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { attest, contextualize } from "@ark/attest"
import { type } from "arktype"
import { configure, type } from "arktype"

contextualize(() => {
it("preserves the original references if no morphs are present", () => {
Expand Down Expand Up @@ -93,4 +93,24 @@ contextualize(() => {
// because it's an exotic object? seems like a Node bug
attest({ ...process.env }).equals(originalEnv)
})

it("can be globally configured", () => {
configure({ clone: false })

const { userForm } = type.module({
userForm: {
age: "string.numeric.parse"
}
})

const formData = {
age: "42"
}

const out = userForm(formData)

// the original object's age key is now a number
attest(formData.age).unknown.equals(42)
attest(formData).unknown.equals(out)
})
})
1 change: 1 addition & 0 deletions ark/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
} from "@ark/schema"
export { Hkt, inferred } from "@ark/util"
export type { distill } from "./attributes.ts"
export * from "./config.ts"
export { Generic } from "./generic.ts"
export {
ark,
Expand Down

0 comments on commit c40ea7c

Please sign in to comment.