const PhoneNumberWithCountryCode = Schema.Struct({
number: Schema.String.annotations({ message: () => "Please enter a valid phone number" }),
countryCode: Schema.String.annotations({ message: () => "Please select a country" }),
})
const PhoneNumberFieldSchema = Schema.transformOrFail(
PhoneNumberWithCountryCode,
Schema.Struct({ phoneNumber: User.PhoneNumber }),
{
decode: (input, _, ast) => {
const result = phone(input.number, { country: input.countryCode })
if (result.isValid) {
return ParseResult.succeed({ phoneNumber: result.phoneNumber })
}
return ParseResult.fail(new ParseResult.Type(ast, input, "Invalid phone number"))
},
encode: (input, _, ast) => {
const result = phone(input.phoneNumber)
if (result.isValid) {
return ParseResult.succeed({ countryCode: result.countryIso3, number: result.phoneNumber })
}
return ParseResult.fail(new ParseResult.Type(ast, input, "Invalid phone number"))
},
},
)
const FormSchema = Schema.extend(
PhoneNumberFieldSchema,
Schema.Struct({
latitude: Schema.NumberFromString.pipe(Schema.compose(Location.Latitude)).annotations({
message: () => "You need to share your location to check in",
}),
longitude: Schema.NumberFromString.pipe(Schema.compose(Location.Longitude)).annotations({
message: () => "You need to share your location to check in",
}),
rules: Schema.Literal("agree").annotations({
message: () => "You must agree to the terms and conditions",
}),
}),
)
const PhoneNumberWithCountryCode = Schema.Struct({
number: Schema.String.annotations({ message: () => "Please enter a valid phone number" }),
countryCode: Schema.String.annotations({ message: () => "Please select a country" }),
})
const PhoneNumberFieldSchema = Schema.transformOrFail(
PhoneNumberWithCountryCode,
Schema.Struct({ phoneNumber: User.PhoneNumber }),
{
decode: (input, _, ast) => {
const result = phone(input.number, { country: input.countryCode })
if (result.isValid) {
return ParseResult.succeed({ phoneNumber: result.phoneNumber })
}
return ParseResult.fail(new ParseResult.Type(ast, input, "Invalid phone number"))
},
encode: (input, _, ast) => {
const result = phone(input.phoneNumber)
if (result.isValid) {
return ParseResult.succeed({ countryCode: result.countryIso3, number: result.phoneNumber })
}
return ParseResult.fail(new ParseResult.Type(ast, input, "Invalid phone number"))
},
},
)
const FormSchema = Schema.extend(
PhoneNumberFieldSchema,
Schema.Struct({
latitude: Schema.NumberFromString.pipe(Schema.compose(Location.Latitude)).annotations({
message: () => "You need to share your location to check in",
}),
longitude: Schema.NumberFromString.pipe(Schema.compose(Location.Longitude)).annotations({
message: () => "You need to share your location to check in",
}),
rules: Schema.Literal("agree").annotations({
message: () => "You must agree to the terms and conditions",
}),
}),
)