Gathering detailed insights and metrics for @sanity-typed/faker
Gathering detailed insights and metrics for @sanity-typed/faker
Gathering detailed insights and metrics for @sanity-typed/faker
Gathering detailed insights and metrics for @sanity-typed/faker
Completing sanity's developer experience with typescript (and more)!
npm install @sanity-typed/faker
Typescript
Module System
Min. Node Version
Node Version
NPM Version
@sanity-typed/next-sanity@4.0.2
Updated on May 27, 2025
@sanity-typed/groq@3.0.2
Updated on May 27, 2025
@sanity-typed/faker@4.0.2
Updated on May 27, 2025
@sanity-typed/client-mock@4.0.2
Updated on May 27, 2025
@sanity-typed/client@5.0.2
Updated on May 27, 2025
@sanity-typed/groq-js@3.0.2
Updated on May 27, 2025
TypeScript (99.94%)
JavaScript (0.06%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
158 Stars
1,706 Commits
8 Forks
3 Watchers
5 Branches
5 Contributors
Updated on Jul 10, 2025
Latest Version
4.0.2
Package Id
@sanity-typed/faker@4.0.2
Unpacked Size
93.97 kB
Size
20.77 kB
File Count
7
NPM Version
10.8.2
Node Version
20.19.1
Published on
May 27, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
6
2
Generate Mock Data from Sanity Schemas
sanityDocumentsFaker
1npm install sanity @faker-js/faker @sanity-typed/faker
product.ts
:
1// import { defineArrayMember, defineField, defineType } from "sanity"; 2import { 3 defineArrayMember, 4 defineField, 5 defineType, 6} from "@sanity-typed/types"; 7 8/** No changes using defineType, defineField, and defineArrayMember */ 9export const product = defineType({ 10 name: "product", 11 type: "document", 12 title: "Product", 13 fields: [ 14 defineField({ 15 name: "productName", 16 type: "string", 17 title: "Product name", 18 validation: (Rule) => Rule.required(), 19 }), 20 defineField({ 21 name: "tags", 22 type: "array", 23 title: "Tags for item", 24 of: [ 25 defineArrayMember({ 26 type: "object", 27 name: "tag", 28 fields: [ 29 defineField({ type: "string", name: "label" }), 30 defineField({ type: "string", name: "value" }), 31 ], 32 }), 33 ], 34 }), 35 ], 36});
sanity.config.ts
:
1import { structureTool } from "sanity/structure"; 2 3// import { defineConfig } from "sanity"; 4import { defineConfig } from "@sanity-typed/types"; 5import type { InferSchemaValues } from "@sanity-typed/types"; 6 7import { post } from "./schemas/post"; 8import { product } from "./schemas/product"; 9 10/** No changes using defineConfig */ 11const config = defineConfig({ 12 projectId: "59t1ed5o", 13 dataset: "production", 14 plugins: [structureTool()], 15 schema: { 16 types: [ 17 product, 18 // ... 19 post, 20 ], 21 }, 22}); 23 24export default config; 25 26/** Typescript type of all types! */ 27export type SanityValues = InferSchemaValues<typeof config>; 28/** 29 * SanityValues === { 30 * product: { 31 * _createdAt: string; 32 * _id: string; 33 * _rev: string; 34 * _type: "product"; 35 * _updatedAt: string; 36 * productName: string; 37 * tags?: { 38 * _key: string; 39 * _type: "tag"; 40 * label?: string; 41 * value?: string; 42 * }[]; 43 * }; 44 * // ... all your types! 45 * } 46 */
mocks.ts
:
1import { base, en } from "@faker-js/faker"; 2import config from "sanity.config"; 3 4import { sanityConfigToFaker, sanityDocumentsFaker } from "@sanity-typed/faker"; 5 6export const getMockDataset = async () => { 7 const sanityFaker = sanityConfigToFaker(config, { 8 faker: { locale: [en, base] }, 9 }); 10 /** 11 * typeof sanityFaker === { 12 * [type in keyof SanityValues]: () => SanityValues[type]; 13 * } 14 */ 15 16 const documentsFaker = sanityDocumentsFaker(config, sanityFaker); 17 /** 18 * typeof documentsFaker === () => SanityValues[keyof SanityValues][] 19 */ 20 21 return documentsFaker(); 22};
sanityDocumentsFaker
While sanityConfigToFaker
gives you all the fakers for a given config keyed by type, sometimes you just want an array of all the SanityDocument
s. Drop it into sanityDocumentsFaker
:
1import type { 2 sanityConfigToFaker, 3 sanityDocumentsFaker, 4} from "@sanity-typed/zod"; 5 6const config = defineConfig({ 7 /* ... */ 8}); 9 10const fakers = sanityConfigToFaker(config); 11/** 12 * fakers === { [type: string]: () => typeButSomeTypesArentDocuments } 13 */ 14 15const documentsFaker = sanityDocumentsFaker(config, fakers); 16/** 17 * documentsFaker === () => (Each | Document | In | An | Array | Many | Times)[] 18 */
Reference mocks point to document mocks, so you can use groq-js
or @sanity-typed/groq-js
and be certain that your references will work.
This is done in chunks of 5. For example, if foo
has references that point to bar
, you can be assured that the the first five mocks of foo
has references that all refer to the first five mocks of bar
. In other words, if you generate five foo
mocks and five bar
mocks, then all the references will be contained within those documents. If you want to change this number, pass a different referencedChunkSize
to sanityConfigToFaker(config, { faker, referencedChunkSize })
.
The tests for this are a good explanation.
As much as is reasonable, a field's mocked values should stay consistent between runs of your application. This is why the faker
parameter accepts a FakerOptions
rather than a Faker
: each field instantiates it's own Faker
with a seed corresponding to the field's path. This means that, even when you change all the fields or array members around a field, that field will produce the same mocked values, as long as the path to it stays consistent. This becomes especially important when you're using slug
for url paths, since you don't want your urls to change every time you change your schema. However, whenever you update your dependencies, we can't ensure that we generated mocks the same way, so don't be surprised if you see some changes in your mocked data.
The tests for this are a good explanation.
If there are custom mocks you want to include, using customMock
on the schema types includes it:
1import { customMock, sanityConfigToFaker } from "@sanity-typed/faker"; 2import { defineConfig, defineField, defineType } from "@sanity-typed/types"; 3 4export const product = defineType({ 5 name: "product", 6 type: "document", 7 title: "Product", 8 fields: [ 9 customMock( 10 defineField({ 11 name: "productName", 12 type: "string", 13 title: "Product name", 14 }), 15 // `previous` is what the mocked value would have been, which is helpful 16 // to have when you only want to override one field in an object 17 // `index` is the index of the mock overall, helping with the reference validity 18 // This is helpful if you want to override slugs, eg always having index === 0 19 // give a locally consistent url 20 (faker, previous, index) => faker.commerce.productName() 21 ), 22 // ... 23 ], 24}); 25 26// Everything else the same as before... 27const config = defineConfig({ 28 projectId: "your-project-id", 29 dataset: "your-dataset-name", 30 schema: { 31 types: [ 32 product, 33 // ... 34 ], 35 }, 36}); 37 38const sanityFaker = sanityConfigToFaker(config, { 39 faker: { locale: [en, base] }, 40}); 41 42const mock = sanityFaker.product(); 43// mock.productName is something like "Computer" rather than the default from "string"
Be aware that, besides typing, no validations or checks are done on the custom mocks. The validity of your custom mocked values are up to you.
@sanity-typed/*
generally has the goal of only having effect to types and no runtime effects. This package is an exception. This means that you will have to import your sanity config to use this. While sanity v3 is better than v2 at having a standard build environment, you will have to handle any nuances, including having a much larger build.
Often you'll run into an issue where you get typescript errors in your IDE but, when building workspace (either you studio or app using types), there are no errors. This only occurs because your IDE is using a different version of typescript than the one in your workspace. A few debugging steps:
JavaScript and TypeScript Nightly
extension (identifier ms-vscode.vscode-typescript-next
) creates issues here by design. It will always attempt to use the newest version of typescript instead of your workspace's version. I ended up uninstalling it..vscode/settings.json
. Use TypeScript: Select TypeScript Version
to explictly pick the workspace version.The supported Typescript version is now 5.7.2 <= x <= 5.7.3. Older versions are no longer supported and newer versions will be added as we validate them.
The supported Typescript version is now 5.4.2 <= x <= 5.6.3. Older versions are no longer supported and newer versions will be added as we validate them.
No vulnerabilities found.
No security vulnerabilities found.