JameEnder
JameEnder
Explore posts from servers
Aarktype
Created by JameEnder on 9/22/2024 in #questions
Automatic "full" discriminated union
Hello, I have this code:
const ActorInputSchema = type({
runMode: '"actor"',
actorName: 'string',
actorInput: 'unknown',
'datasetId?': 'never',
'exactData?': 'never',
})

const DatasetInputSchema = type({
runMode: '"dataset"',
'actorName?': 'never',
'actorInput?': 'never',
datasetId: 'string',
'exactData?': 'never',
})

const ExactInputSchema = type({
runMode: '"exact"',
'actorName?': 'never',
'actorInput?': 'never',
'datasetId?': 'never',
exactData: 'string'
})

const InputSchema = ActorInputSchema.or(DatasetInputSchema).or(ExactInputSchema)

type Input = typeof InputSchema.infer;

const input = InputSchema(/* some data */);

if (input instanceof type.errors) {
console.error(input.message); process.exit();
}

const {
runMode,
// Property actorName does not exist on type...
actorName,
actorInput,
datasetId,
exactData,
} = input as Input;
const ActorInputSchema = type({
runMode: '"actor"',
actorName: 'string',
actorInput: 'unknown',
'datasetId?': 'never',
'exactData?': 'never',
})

const DatasetInputSchema = type({
runMode: '"dataset"',
'actorName?': 'never',
'actorInput?': 'never',
datasetId: 'string',
'exactData?': 'never',
})

const ExactInputSchema = type({
runMode: '"exact"',
'actorName?': 'never',
'actorInput?': 'never',
'datasetId?': 'never',
exactData: 'string'
})

const InputSchema = ActorInputSchema.or(DatasetInputSchema).or(ExactInputSchema)

type Input = typeof InputSchema.infer;

const input = InputSchema(/* some data */);

if (input instanceof type.errors) {
console.error(input.message); process.exit();
}

const {
runMode,
// Property actorName does not exist on type...
actorName,
actorInput,
datasetId,
exactData,
} = input as Input;
But in this case, I have to add field?: 'never' to make the compiler happy when destructuring the validated input, else it says that some field doesn't exist on it. Is there a way to make this work, or is my approach wrong from the start? Thank you! :D
23 replies
Aarktype
Created by JameEnder on 5/31/2024 in #questions
Mapping types
Hello, is it possible do to code like this?
const x = type({
a: 'string',
b: 'string',
c: 'string'
})

const xType = typeof x;

const y = x.map((original) => ({
...original,
c: 'number'
}))

const yType = typeof y;
const x = type({
a: 'string',
b: 'string',
c: 'string'
})

const xType = typeof x;

const y = x.map((original) => ({
...original,
c: 'number'
}))

const yType = typeof y;
And if so, is it possible to also inherit those types while mapping in logic, like so?
const y = x.logicMap((original) => ({
...original,
c: Number(original.c)
}))

const yType = typeof y;
const y = x.logicMap((original) => ({
...original,
c: Number(original.c)
}))

const yType = typeof y;
146 replies
Aarktype
Created by JameEnder on 5/2/2024 in #questions
Referencing self
Hello, I have a type A
const A = {
hello: 'string',
aArray: arrayOf(???)
}
const A = {
hello: 'string',
aArray: arrayOf(???)
}
How can I reference itself in arrayOf? A doesn't seem to work.
4 replies
Aarktype
Created by JameEnder on 4/12/2024 in #questions
Export type to JSON and share it
Hello, I have two TS programs that should cooperate (A B), B import types and validates data, and A creates and "saves" the types. A rough idea
// Program A
import { type } from 'arktype'

const a = type({
name: 'string'
})

const b = type({
userData: a,
random: 'number'
})

// Somehow export the type as JSON?
fs.writeFileSync("schema.json", JSON.stringify(b))
// Program A
import { type } from 'arktype'

const a = type({
name: 'string'
})

const b = type({
userData: a,
random: 'number'
})

// Somehow export the type as JSON?
fs.writeFileSync("schema.json", JSON.stringify(b))
// Program B
import { type } from 'arktype'

const definition = JSON.stringify(fs.readFileSync("schema.json").toString())

const validator = type(definition)

const { data, problems } = validator({
userData: {
name: "Jacob"
},
random: 5
})
// Program B
import { type } from 'arktype'

const definition = JSON.stringify(fs.readFileSync("schema.json").toString())

const validator = type(definition)

const { data, problems } = validator({
userData: {
name: "Jacob"
},
random: 5
})
Is this possible with the current API?
119 replies