Job schema for Sanity and Astro
A vacancy: what the job is, where, on what terms, and where to apply. Pay, shifts, the hiring employer and the team are extensions.
- Core fields
- 6
- Extensions
- 4 (10 fields)
- schema.org
- JobPosting
- Licence
- MIT
Free, with no account. Same data in both editions: the Astro entry id is the Sanity slug.
Core fields
What every job has. A slug is added in Sanity.
| Field | Type | Required | What it holds |
|---|---|---|---|
| title | string | Yes | Title |
| location | string | Yes | WhereTown, or Remote. |
| type | string | Yes | ContractFull-time, Part-time, Temporary. |
| summary | text | No | Summary |
| posted | date | No | Posted |
| href | url | Yes | Apply linkWhere to apply: the job’s own page (/jobs/electrician/) or the applicant system. |
Extensions
Fields for one kind of site, on top of the core. An extension never changes a core field, and there are no settings: a different need is a different extension.
employer: Hiring employer
For an agency or a job board: who the job is with. Fits recruitment, job boards.
| Field | Type | Required | What it holds |
|---|---|---|---|
| employer | string | Yes | Employer |
| employerLine | string | No | About the employer |
pay: Pay
The pay range and what it is per. Fits recruitment, trades, hospitality.
| Field | Type | Required | What it holds |
|---|---|---|---|
| payFrom | number | No | From |
| payTo | number | No | To |
| payPer | one of hour, day, month, year | No | Per |
shift: Hours and shifts
For work on site: the hours, the shift pattern, the level and the extras. Fits recruitment, trades, care, logistics.
| Field | Type | Required | What it holds |
|---|---|---|---|
| hours | string | No | Hours40 a week. |
| shift | string | No | ShiftsDays, no weekends. |
| level | string | No | Level |
| extras | list of string | No | Extras |
team: Team
For a company's own careers page: the team the job is in, the careers section's grouping. Fits software, agencies.
| Field | Type | Required | What it holds |
|---|---|---|---|
| team | string | Yes | Team |
Get the code
Plain TypeScript with no package of ours: the files become your code. Tested in a new Sanity Studio and a new Astro project.
For your Studio: the document type, the same fields as an inline object, a GROQ projection and a list query, and the TypeScript type the projection returns.
Install with the shadcn CLI
npx shadcn@latest add https://www.themes.dev/sanity/r/job.jsonThe CLI needs a components.json in the project. No CLI? Copy the files below: they are all there is.
// Sanity schema: job v1.// From themes.dev content models (https://www.themes.dev/sanity/models/), MIT. Your code now: edit it freely.import { defineArrayMember, defineField, defineType } from 'sanity'; const fields = [ defineField({ name: 'title', title: 'Title', type: 'string', validation: (rule) => rule.required().max(60) }), defineField({ name: 'location', title: 'Where', description: 'Town, or Remote.', type: 'string', validation: (rule) => rule.required().max(40) }), defineField({ name: 'type', title: 'Contract', description: 'Full-time, Part-time, Temporary.', type: 'string', validation: (rule) => rule.required().max(30) }), defineField({ name: 'summary', title: 'Summary', type: 'text', rows: 2, validation: (rule) => rule.max(300) }), defineField({ name: 'posted', title: 'Posted', type: 'date' }), defineField({ name: 'href', title: 'Apply link', description: 'Where to apply: the job’s own page (/jobs/electrician/) or the applicant system.', type: 'url', validation: (rule) => rule.required().uri({ allowRelative: true, scheme: ['http', 'https', 'mailto', 'tel'] }) }),]; /** The id other entries refer to, and the Astro edition's entry id: the same string in both. */const slug = defineField({ name: 'slug', title: 'Slug', type: 'slug', description: 'Lowercase letters, digits and hyphens.', options: { source: 'title', maxLength: 64 }, validation: (rule) => rule.required() }); /** A vacancy: what the job is, where, on what terms, and where to apply. Pay, shifts, the hiring employer and the team are extensions. */export const job = defineType({ name: 'job', title: 'Job', type: 'document', fields: [fields[0], slug, ...fields.slice(1)], orderings: [{ title: 'Newest first', name: 'posted', by: [{ field: 'posted', direction: 'desc' }] }], preview: { select: { title: 'title', subtitle: 'location' } },}); /** The same fields as an object, for a page that keeps its own jobs inline. */export const jobEntry = defineType({ name: 'jobEntry', title: 'Job', type: 'object', fields, preview: { select: { title: 'title', subtitle: 'location' } },}); /** GROQ: one job as the JobRecord type (types/job.ts) describes it. */export const jobProjection = `{ "id": slug.current, title, location, type, summary, posted, href }`; /** GROQ: every job, in order. */export const jobListQuery = `*[_type == "job" && defined(slug.current)] | order(posted desc, _createdAt asc) ${jobProjection}`;// TypeScript types: job v1.// From themes.dev content models (https://www.themes.dev/sanity/models/), MIT. Your code now: edit it freely. /** A vacancy: what the job is, where, on what terms, and where to apply. Pay, shifts, the hiring employer and the team are extensions. */export interface Job { title: string; /** Town, or Remote. */ location: string; /** Full-time, Part-time, Temporary. */ type: string; summary?: string; posted?: string; /** Where to apply: the job’s own page (/jobs/electrician/) or the applicant system. */ href: string;} /** What the GROQ projection returns: the fields and the id (the slug, the same as the Astro entry's id). */export type JobRecord = Job & { id: string };// Shared shapes of the generated content models./** An image: the path or URL without size suffix, and its alternative text (empty when the text beside it says it). */export interface ImageRef { src: string; alt: string; /** Intrinsic size, so the page reserves the box before the file arrives. */ width: number; height: number;} export interface Link { label: string; href: string;} /** Portable Text as Sanity stores it: blocks, and images with their alt text. */export type RichText = { _type: string; _key: string; [key: string]: unknown }[];Use it
In Sanity
// sanity.config.tsimport { job, jobEntry } from './schemaTypes/job'; export default defineConfig({ // … schema: { types: [job, jobEntry] },}); // In your site: the projection returns the Job type in types/job.ts.import { jobListQuery } from './schemaTypes/job';const jobs = await client.fetch(jobListQuery);In Astro
// src/content.config.tsimport { defineCollection } from 'astro:content';import { file } from 'astro/loaders';import { jobSchema } from './models/job'; const jobs = defineCollection({ loader: file('./src/content/jobs.json'), schema: jobSchema,}); export const collections = { jobs };An image is always { src, alt, width, height } in both editions: the GROQ projection reads the size from the asset, and in Astro it sits in your data, so pages never shift while they load.