type Int = int type Optional = { score?: Int } type Required = { score: Int } service Read { next(value: Int): Int } service Defaults { make(): Int } export const supervised = (): Int => scoped(defer(() => { forkScoped(defer(() => 3)) 3 })) export const timedBatches = (source: Stream): Stream => aggregateWithinStream(source, takeSink(2), scheduleSpaced(1000)) const integer: Schema = schema() const optional: SchemaOptional = schemaOptionalKey(integer) const shiftedField: SchemaOptional = schemaOptionalTransform( optional, optional, (value: Option): Option => match value { Some(score) => Some(Read.next(score)), None => None }, (value: Option): Option => value ) const shifted: Schema = schemaStruct({ score: shiftedField }) const defaulted: Schema = schemaStruct({ score: schemaDefaultKey(integer, (): Int => Defaults.make()) }) export const readShifted = (input: unknown): Optional fails SchemaError uses Read.next => schemaDecode(shifted, input) export const readDefault = (input: unknown): Required fails SchemaError uses Defaults.make => schemaDecode(defaulted, input) type Name = string type OptionalName = { name?: Name } type RequiredName = { name: Name } const name: Schema = schema() const optionalName: SchemaOptional = schemaOptionalKey(name) const requiredNameField: SchemaDecodedOptional = schemaOptionalFromRequired( name, optionalName, (value: Name): Option => if (value === "") { None } else { Some(value) }, (value: Option): Name => match value { Some(current) => current, None => "" } ) export const names: Schema = schemaStruct({ name: requiredNameField }) type Scalar = Name | Int export const scalar: Schema = schemaUnion([name, integer]) service Validation { accept(value: Name): Name fails SchemaIssue } const checkedName: Schema = schemaTransform( name, name, (value: Name): Name => Validation.accept(value), (value: Name): Option => Some(value) ) export const fallbackName: Schema = schemaUnion([checkedName, name]) type Nil = null type Nullable = Name | Nil type NullableRecord = { name?: Nullable } const nil: Schema = schema() const nullable: Schema = schemaUnion([name, nil]) export const preserveNull: Schema = schemaStruct({ name: schemaOptionalKey(nullable) }) export const omitNull: Schema = schemaStruct({ name: schemaNullToMissingKey(name) }) export const missingName = (): NullableRecord => ({}) export const clearedName = (): NullableRecord => ({ name: null }) export const omittedDefault: Schema = schemaStruct({ name: schemaDefaultKey(name, (): Name => "Anonymous", { encodingStrategy: "omit" }) }) export const passedDefault: Schema = schemaStruct({ name: schemaDefaultKey(name, (): Name => "Anonymous", { encodingStrategy: "passthrough" }) }) export const defaultedNull: Schema = schemaStruct({ name: schemaDefaultKey(name, (): Name => "Anonymous", { on: "null" }) }) export const readMissingName = (): RequiredName fails SchemaError => schemaDecode(defaultedNull, {}) export const readClearedName = (): RequiredName fails SchemaError => schemaDecode(defaultedNull, { name: null }) type Text = string type RequiredScore = { score: Int } type WireRecord = { score?: Text } service Wire { parse(value: Text): Int fails SchemaIssue print(value: Int): Text fails SchemaIssue } const wireText: Schema = schema() const wireScore: Schema = schemaTransform( wireText, integer, (value: Text): Int => Wire.parse(value), (value: Int): Text => Wire.print(value) ) export const encodedDefault: Schema = schemaStruct({ score: schemaDefaultKey(wireScore, (): Text => "41", { on: "encoded", encodingStrategy: "omit" }) }) type ConstructorInput = { score?: Int } type ConstructorRecord = { score: Int } const constructorRecord: Schema = schemaStruct({ score: integer }) export const makeScore = (input: ConstructorInput): ConstructorRecord fails SchemaIssue => { const score = match input.score { Some(value) => value, None => 4 } schemaMake(constructorRecord, { score: score }) } type DefaultRecord = { score: Int } type DefaultInput = { score?: Int } type NestedRecord = { settings: DefaultRecord } type NestedInput = { settings?: DefaultInput } export const constructorDefaults: Schema = schemaStruct({ score: schemaConstructorDefault(integer, (): Int => 4) }) export const nestedDefaults: Schema = schemaStruct({ settings: schemaConstructorDefault(constructorDefaults, (): DefaultInput => ({})) }) export const constructorDefaultsList: Schema = schemaArray(constructorDefaults) export const constructDefaults = (input: NestedInput): NestedRecord fails SchemaIssue => schemaMake(nestedDefaults, input) type SettingsRecord = { settings: DefaultRecord } type SettingsWire = { settings?: DefaultRecord } type SettingsInput = { settings: DefaultInput } type OptionalSettingsInput = { settings?: DefaultInput } type NullableSettingsWire = { settings?: DefaultRecord | null } export const wireDefaultSettings: Schema = schemaStruct({ settings: schemaDefaultKey(constructorDefaults, (): DefaultRecord => ({ score: 8 })) }) export const optionalSettings: Schema = schemaStruct({ settings: schemaNullToMissingKey(constructorDefaults) }) export const constructSettings = (input: SettingsInput): SettingsRecord fails SchemaIssue => schemaMake(wireDefaultSettings, input) type Tree = { score: Int, children: Tree[] } type TreeInput = { score?: Int, children?: TreeInput[] } export const treeDefaults: Schema = schemaRecursive( (self: Schema): Schema => schemaStruct({ score: schemaConstructorDefault(integer, (): Int => 4), children: schemaConstructorDefault(schemaArray(self), (): TreeInput[] => []) }) ) export const constructTree = (input: TreeInput): Tree fails SchemaIssue => schemaMake(treeDefaults, input) type WireTree = { score: Text, children: WireTree[] } export const wireTree: Schema = schemaRecursive( (self: Schema): Schema => schemaStruct({ score: schemaConstructorDefault(wireScore, (): Int => 4), children: schemaConstructorDefault(schemaArray(self), (): TreeInput[] => []) }) ) export const readWireTree = (input: unknown): Tree fails SchemaError uses Wire.parse => schemaDecode(wireTree, input) export const writeWireTree = (input: Tree): WireTree fails SchemaError uses Wire.print => schemaEncode(wireTree, input) export const constructWireTree = (input: TreeInput): Tree fails SchemaIssue => schemaMake(wireTree, input) export const dualDefaultField: SchemaEncodedOptionalConstructorDefault = schemaConstructorDefault(schemaDefaultKey(integer, (): Int => 8), (): Int => 4) export const dualDefaultScores: Schema = schemaStruct({ score: dualDefaultField }) export const optionalDefaultField: SchemaOptionalConstructorDefault = schemaConstructorDefault(schemaOptionalKey(integer), (): Int => 4) export const optionalDefaultScores: Schema = schemaStruct({ score: optionalDefaultField }) export const makeDualDefaults = (input: Optional): Required fails SchemaIssue => schemaMake(dualDefaultScores, input) type OptionScores = { score: Option } const constructorFirst: SchemaConstructorDefault = schemaConstructorDefault(integer, (): Int => 4) export const adapterAfterDefault: SchemaEncodedOptionalConstructorDefault = schemaDefaultKey(constructorFirst, (): Int => 8) export const adapterAfterDefaults: Schema = schemaStruct({ score: adapterAfterDefault }) export const optionAfterDefaults: Schema = schemaStruct({ score: schemaOptionFromOptionalKey(constructorFirst) }) export const makeAdapterAfterDefault = (input: Optional): Required fails SchemaIssue => schemaMake(adapterAfterDefaults, input) export const makeAdaptedOption = (input: OptionScores): OptionScores fails SchemaIssue => schemaMake(optionAfterDefaults, input) type DefaultWireTree = { score: Int, children?: DefaultWireTree[] } export const defaultedTree: Schema = schemaRecursive( (self: Schema): Schema => schemaStruct({ score: schemaConstructorDefault(integer, (): Int => 4), children: schemaDefaultKey( schemaConstructorDefault(schemaArray(self), (): TreeInput[] => []), (): Tree[] => [{ score: 8, children: [] }]) }) ) export const makeDefaultedTree = (input: TreeInput): Tree fails SchemaIssue => schemaMake(defaultedTree, input) export const readDefaultedTree = (input: unknown): Tree fails SchemaError => schemaDecode(defaultedTree, input)