Last active
December 30, 2021 23:21
-
-
Save HonzaMac/d2889f3c86f3ee140beed8c28b2f6674 to your computer and use it in GitHub Desktop.
faker entity via yup schema
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { User, userSchema } from "../../src/userService" | |
import { fake } from "yup-schema-faker" | |
export const createUser = (data: Partial<User> = {}): User => { | |
const fakeSchemaData = fake(userSchema.noUnknown()) as User; | |
return { | |
...fakeSchemaData, | |
...data, | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { InferType, object, string } from "yup" | |
export const userSchema = object({ | |
id: string().uuid(), | |
username: string().required(), | |
email: string().email().required(), | |
country: string().required(), | |
department: string().required(), | |
divisionName: string().required(), | |
city: string().optional(), | |
}).required(); | |
type UserSchema = InferType<typeof userSchema>; | |
export type User = UserSchema; | |
export const updateUser = (user: User, updateData: Partial<User>): User => { | |
const validUser = userSchema.validateSync(user) | |
return { | |
...validUser, | |
...updateData, | |
}}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment