Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip #73

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

wip #73

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/vue/src/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type NestedFieldInfoKey<Key> = [Key] extends [Record<PropertyKey, any>]

type DistributiveNestedFieldInfoKey<Key> = Key extends any ? NestedFieldInfoKey<Key> : never

// IDEA: keep track of both From and To types, maybe we can gather more information
export type NestedFieldInfo<To extends Record<PropertyKey, any>> = // exploit eventual _tag field to propagate the unique tag
{
fields: {
Expand Down Expand Up @@ -110,7 +111,15 @@ function handlePropertySignature(
propertySignature.annotations
)
)
: buildFieldInfo(propertySignature)
: buildFieldInfo(
new S.AST.PropertySignature(
propertySignature.name,
schema.ast.from,
propertySignature.isOptional,
propertySignature.isReadonly,
propertySignature.annotations
)
)
}
case "TypeLiteral": {
return buildFieldInfoFromFieldsRoot(
Expand Down Expand Up @@ -260,7 +269,10 @@ function buildFieldInfo(
?? (Option.isSome(id2) ? customSchemaErrors.value.get(id2.value) : undefined)

return custom ? custom(err, e, v) : translate.value(
{ defaultMessage: "The entered value is not a valid {type}: {message}", id: "validation.not_a_valid" },
{
defaultMessage: `The entered value is not a valid {type}: {message}`,
id: "validation.not_a_valid"
},
{
type: translate.value({
defaultMessage: capitalize(propertyKey.toString()),
Expand Down
22 changes: 14 additions & 8 deletions packages/vue/test/form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Effect } from "@effect-app/core"
import { S } from "effect-app"
import type { DiscriminatedUnionFieldInfo, FieldInfo, NestedFieldInfo, UnionFieldInfo } from "../src/form.js"
import { buildFieldInfoFromFieldsRoot } from "../src/form.js"
import { Password, type PasswordSecret } from "./password.test.js"

export class NestedSchema extends S.Class<NestedSchema>()({
shallow: S.string,
Expand All @@ -11,9 +12,12 @@ export class NestedSchema extends S.Class<NestedSchema>()({
deepest: S.number
})
}),
age: S.propertySignature(S.struct({ nfs: S.NumberFromString.pipe(S.compose(S.PositiveInt)) }))
password: Password
}) {}

type NestedSchemaFrom = S.Schema.Encoded<typeof NestedSchema>
type NestedSchemaTo = S.Schema.Type<typeof NestedSchema>

export class SchemaContainsClass extends S.Class<SchemaContainsClass>()({
inner: NestedSchema
}) {}
Expand Down Expand Up @@ -162,10 +166,13 @@ it("buildFieldInfo", () =>
Effect
.gen(function*() {
const nestedFieldinfo = buildFieldInfoFromFieldsRoot(NestedSchema)
console.log((nestedFieldinfo.fields.password as any).rules[1]("5"))

expectTypeOf(nestedFieldinfo).toEqualTypeOf<NestedFieldInfo<NestedSchema>>()
expectTypeOf(nestedFieldinfo.fields.shallow).toEqualTypeOf<FieldInfo<string>>()
expectTypeOf(nestedFieldinfo.fields.age).toEqualTypeOf<NestedFieldInfo<NestedSchema["age"]>>()
expectTypeOf(nestedFieldinfo.fields.age.fields.nfs).toEqualTypeOf<FieldInfo<number & S.PositiveIntBrand>>()

// it should be a FieldInfo of something
expectTypeOf(nestedFieldinfo.fields.password).toEqualTypeOf<NestedFieldInfo<PasswordSecret>>()
expectTypeOf(nestedFieldinfo.fields.nested).toEqualTypeOf<NestedFieldInfo<NestedSchema["nested"]>>()
expectTypeOf(nestedFieldinfo.fields.nested.fields.deep).toEqualTypeOf<FieldInfo<string & S.NonEmptyStringBrand>>()
expectTypeOf(nestedFieldinfo.fields.nested.fields.nested).toEqualTypeOf<
Expand All @@ -176,7 +183,7 @@ it("buildFieldInfo", () =>
// it's a recursive check on actual runtime structure
testNestedFieldInfo(nestedFieldinfo)
testNestedFieldInfo(nestedFieldinfo.fields.nested)
testNestedFieldInfo(nestedFieldinfo.fields.age)
testNestedFieldInfo(nestedFieldinfo.fields.password)
})
.pipe(Effect.runPromise))

Expand All @@ -190,7 +197,7 @@ it("buildFieldInfo schema containing class", () =>
testNestedFieldInfo(fieldinfo.fields.inner)
testNestedFieldInfo(fieldinfo.fields.inner.fields.nested.fields.nested)
})
.pipe(Effect.runPromise))
.pipe(Effect.runPromise), { skip: true })

it("buildFieldInfo with simple union", () =>
Effect
Expand Down Expand Up @@ -218,11 +225,10 @@ it("buildFieldInfo with simple union", () =>
testNestedFieldInfo(unionFieldinfo)
testFieldInfo(unionFieldinfo.fields.nullable)
testFieldInfo(unionFieldinfo.fields.optional)
console.log({ asd: unionFieldinfo.fields.structsUnion })
testUnionFieldInfo(unionFieldinfo.fields.structsUnion)
testFieldInfo(unionFieldinfo.fields.generalUnion)
})
.pipe(Effect.runPromise))
.pipe(Effect.runPromise), { skip: true })

it("buildFieldInfo with tagged unions", () =>
Effect
Expand Down Expand Up @@ -259,4 +265,4 @@ it("buildFieldInfo with tagged unions", () =>
expect(shapeFieldinfo.fields.shapeWithStruct.members.CircleStruct._infoTag).toBe("CircleStruct")
testFieldInfo(shapeFieldinfo.fields.shapeWithStruct.members.CircleStruct.fields.radius)
})
.pipe(Effect.runPromise))
.pipe(Effect.runPromise), { skip: true })
48 changes: 48 additions & 0 deletions packages/vue/test/password.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { S, Secret } from "effect-app"
import type { Schema } from "effect-app/schema"
import { transform, withDefaults } from "effect-app/schema"
import type { Brand } from "effect/Brand"

export interface PasswordBrand extends Brand<"Password"> {
}

export type Password = string & PasswordBrand

export type PasswordSecret = Secret.Secret & PasswordBrand

const complexity = /(?=^.{8,50}$)(?=.*\d)(?=.*[!@#$%^&*,.]+)(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/
function verifyPasswordComplexity(_: string): _ is Password {
return !!_.match(complexity)
}

const symbols = "!@#$%^&*,."
export const passwordString = S.string.pipe(
S.filter(
verifyPasswordComplexity,
{
message: () =>
"Expected a Password with at least 8 characters, one uppercase, one lowercase, one number and one special character e.g. "
+ symbols
}
)
)

const SecretPassword = S.SecretFromSelf as unknown as Schema<PasswordSecret>

export const Password = Object
.assign(
transform(
passwordString,
SecretPassword,
(str) => Secret.fromString(str),
(secret) => Secret.value(secret),
{ strict: false }
)
.annotations({ identifier: "Password" })
.pipe(withDefaults),
{ symbols }
)

it("should be true", () => {
expect(true).toBe(true)
})