Skip to content

Conversation

@renovate
Copy link
Contributor

@renovate renovate bot commented Feb 23, 2025

This PR contains the following updates:

Package Change Age Adoption Passing Confidence
arktype (source) 2.0.0-dev.12-cjs -> 2.1.0 age adoption passing confidence

Warning

Some dependencies could not be looked up. Check the Dependency Dashboard for more information.


Release Notes

arktypeio/arktype (arktype)

v2.1.0

Compare Source

match

The match function provides a powerful way to handle different types of input and return corresponding outputs based on the input type, like a type-safe switch statement.

Case Record API

The simplest way to define a matcher is with ArkType definition strings as keys with corresponding handlers as values:

import { match } from "arktype"

const sizeOf = match({
	"string | Array": v => v.length,
	number: v => v,
	bigint: v => v,
	default: "assert"
})

// a match definition is complete once a `default` has been specified,
// either as a case or via the .default() method

sizeOf("abc") // 3
sizeOf([1, 2, 3, 4]) // 4
sizeOf(5n) // 5n
// ArkErrors: must be an object, a string, a number or a bigint (was boolean)
sizeOf(true)

In this example, sizeOf is a matcher that takes a string, array, number, or bigint as input. It returns the length of strings and arrays, and the value of numbers and bigints.

default accepts one of 4 values:

  • "assert": accept unknown, throw if none of the cases match
  • "never": accept an input based on inferred cases, throw if none match
  • "reject": accept unknown, return ArkErrors if none of the cases match
  • (data: In) => unknown: handle data not matching other cases directly

Cases will be checked in the order they are specified, either as object literal keys or via chained methods.

Fluent API

The match function also provides a fluent API. This can be convenient for non-string-embeddable definitions:

// the Case Record and Fluent APIs can be easily combined
const sizeOf = match({
	string: v => v.length,
	number: v => v,
	bigint: v => v
})
	// match any object with a numeric length property and extract it
	.case({ length: "number" }, o => o.length)
	// return 0 for all other data
	.default(() => 0)

sizeOf("abc") // 3
sizeOf({ name: "David", length: 5 }) // 5
sizeOf(null) // 0
Narrowing input with in, property matching with at
type Data =
	| {
			id: 1
			oneValue: number
	  }
	| {
			id: 2
			twoValue: string
	  }

const discriminateValue = match
	// .in allows you to specify the input TypeScript allows for your matcher
	.in<Data>()
	// .at allows you to specify a key at which your input will be matched
	.at("id")
	.match({
		1: o => `${o.oneValue}!`,
		2: o => o.twoValue.length,
		default: "assert"
	})

discriminateValue({ id: 1, oneValue: 1 }) // "1!"
discriminateValue({ id: 2, twoValue: "two" }) // 3
discriminateValue({ oneValue: 3 })
Builtin keywords can now be globally configured

This can be very helpful for customizing error messages without needing to create your own aliases or wrappers.

import { configure } from "arktype/config"

configure({
	keywords: {
		string: "shorthand description",
		"string.email": {
			actual: () => "definitely fake"
		}
	}
})
import "./config.ts"
import { type } from "arktype"

const user = type({
	name: "string",
	email: "string.email"
})

const out = user({
	// ArkErrors: name must be shorthand description (was a number)
	name: 5,
	// ArkErrors: email must be an email address (was definitely fake)
	email: "449 Canal St"
})

The options you can provide here are identical to those used to configure a Type directly, and can also be extended at a type-level to include custom metadata.

Tuple and args expressions for .to

If a morph returns an ArkErrors instance, validation will fail with that result instead of it being treated as a value. This is especially useful for using other Types as morphs to validate output or chain transformations.

To make this easier, there's a special to operator that can pipe to a parsed definition without having to wrap it in type to make it a function.

This was added before 2.0, but now it comes with a corresponding operator (|>) so that it can be expressed via a tuple or args like most other expressions:

const fluentStillWorks = type("string.numeric.parse").to("number % 2")

const nowSoDoesTuple = type({
	someKey: ["string.numeric.parse", "|>", "number % 2"]
})

const andSpreadArgs = type("string.numeric.parse", "|>", "number % 2")
Error configurations now accept a string directly
const customOne = type("1", "@&#8203;", {
	// previously only a function returning a string was allowed here
	message: "Yikes."
})

// ArkErrors: Yikes.
customOne(2)

Keep in mind, as mentioned in the docs, error configs like message can clobber more granular config options like expected and actual and cannot be included in composite errors e.g. for a union.

Though generally, returning a string based on context is the best option, in situations where you always want the same static message, it's now easier to get that!

Type.toString() now wraps its syntactic representation in Type<..>

Previously, Type.toString() just returned Type.expression. However, in contexts where the source of a message isn't always a Type, it could be confusing:

// < 2.1.0:  "(was string)"
// >= 2.1.0: "(was Type<string>)"
console.log(`(was ${type.string})`)

Hopefully if you interpolate a Type, you'll be less confused by the result from now on!

Improve how Type instances are inferred when wrapped in external generics

Previously, we used NoInfer in some Type method returns. After migrating those to inlined conditionals, we get the same benefit and external inference for cases like this is more reliable:

function fn<
	T extends {
		schema: StandardSchemaV1
	}
>(_: T) {
	return {} as StandardSchemaV1.InferOutput<T["schema"]>
}

// was inferred as unknown (now correctly { name: string })
const arkRes = fn({
	schema: type({
		name: "string"
	})
})
Fix an issue causing some discriminated unions to incorrectly reject default cases
const discriminated = type({
	id: "0",
	k1: "number"
})
	.or({ id: "1", k1: "number" })
	.or({
		name: "string"
	})

// previously, this was rejected as requiring a "k1" key

// will now hit the case discriminated for id: 1,
// but still correctly be allowed via the { name: string } branch
discriminated({ name: "foo", id: 1 }))

v2.0.4

Compare Source

  • Fix an issue causing global configs to be overwritten when the primary "arktype" entry point is imported:

config.ts

import { configure } from "arktype/config"

configure({ numberAllowsNaN: true })

main.ts

import "./config.ts"
import { type } from "arktype"
// now correctly allows NaN
type.number.allows(Number.NaN)

Previous versions of the docs mistakenly suggested this was possible in a single file. This is not the case in ESM due to hoisting. See the updated global configuration docs here.

  • Better ParseError when attempting to constraint a morph

Previously, attempting to directly constrain a transformed type was not a type error but gave a confusing error at runtime:

// ParseError: MinLength operand must be a string or an array (was never)
type("string.trim > 2")

We've added a type error and improved the runtime error:

// TypeScript: To constrain the output of string.trim, pipe like myMorph.to('number > 0')
// ParseError: MinLength operand must be a string or an array (was a morph)
type("string.trim > 2")
  • Fix an issue causing certain complex morph types to not infer output correctly, e.g.:
const types = type.module({
	From: { a: ["1", "=>", () => 2] },
	Morph: ["From", "=>", e => e],
	To: { a: "2" }
})
const U = types.Morph.pipe(e => e, types.To)

// was:
//    (In: never) => To<{ a: 2 }>
// now fixed to:
//    { a: 2 }
const out = U.assert({ a: 1 })

v2.0.3

Compare Source

  • Fix an issue causing some unions with onUndeclaredKey: "reject" to reject valid data (#​1266)

  • Fix an issue where Types containing arrays were incorrectly treated as including morphs, leading to some unnecessary validation overhead (#​1268)

  • Fix an issue causing objects containing functions like () => never that are subtypes of InferredMorph to be incorrectly treated as morphs (#​1264)

  • Fail early with a ParseError if instanceOf operand is not actually a function at runtime (#​1262)

v2.0.2

Compare Source

  • Fix an issue where type-altering (currently config options numberAllowsNan, dateAllowsInvalid and onUndeclaredKey) could be specified at a scope-level, leading to unintuitive cache results (#​1255)

v2.0.1

Compare Source

  • Fix @ark/util version specifier

v2.0.0

Compare Source

  • Initial stable release 🎉

v2.0.0-rc.33

Compare Source

v2.0.0-rc.32

Compare Source

v2.0.0-rc.31

Compare Source

v2.0.0-rc.30

Compare Source

v2.0.0-rc.29

Compare Source

v2.0.0-rc.28

Compare Source

v2.0.0-rc.27

Compare Source

v2.0.0-rc.26

Compare Source

v2.0.0-rc.25

Compare Source

v2.0.0-rc.24

Compare Source

v2.0.0-rc.23

Compare Source

v2.0.0-rc.22

Compare Source

v2.0.0-rc.21

Compare Source

v2.0.0-rc.18

Compare Source

v2.0.0-rc.17

Compare Source

v2.0.0-rc.16

Compare Source

v2.0.0-rc.15

Compare Source

v2.0.0-rc.14

Compare Source

v2.0.0-rc.13

Compare Source

v2.0.0-rc.12

Compare Source

v2.0.0-rc.11

Compare Source

v2.0.0-rc.10

Compare Source

v2.0.0-rc.9

Compare Source

v2.0.0-rc.8

Compare Source

v2.0.0-rc.7

Compare Source

v2.0.0-rc.6

Compare Source

v2.0.0-rc.5

Compare Source

v2.0.0-rc.4

Compare Source

v2.0.0-rc.3

Compare Source

v2.0.0-rc.2

Compare Source

v2.0.0-rc.1

Compare Source

v2.0.0-rc.0

Compare Source


Configuration

📅 Schedule: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 Automerge: Enabled.

Rebasing: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

🔕 Ignore: Close this PR and you won't be reminded about these updates again.


  • If you want to rebase/retry this PR, check this box

This PR was generated by Mend Renovate. View the repository job log.

@renovate renovate bot added the renovatebot label Feb 23, 2025
@DarkGL DarkGL closed this Feb 24, 2025
@renovate
Copy link
Contributor Author

renovate bot commented Feb 24, 2025

Renovate Ignore Notification

Because you closed this PR without merging, Renovate will ignore this update (2.1.0). You will get a PR once a newer version is released. To ignore this dependency forever, add it to the ignoreDeps array of your Renovate config.

If you accidentally closed this PR, or if you changed your mind: rename this PR to get a fresh replacement PR.

@renovate renovate bot deleted the renovate/arktype-2.x branch February 24, 2025 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants