Skip to content

Commit

Permalink
chore: remove global config
Browse files Browse the repository at this point in the history
  • Loading branch information
zce committed Dec 8, 2023
1 parent e321ced commit de0b6d7
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions src/schemas/slug.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { cache } from '../cache'
import { string } from './zod'

const cache = new Map<string, string>()

/**
* generate a slug schema
* @param by unique by this, used to create a unique set of slugs
Expand All @@ -14,20 +13,9 @@ export const slug = (by: string = 'global', reserved: string[] = []) =>
.max(200)
.regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/i, 'Invalid slug')
.refine(value => !reserved.includes(value), 'Reserved slug')
.superRefine((value, ctx) => {
const key = `schemas:slug:${by}:${value}`
const path = ctx.meta.file.path + ':' + ctx.path.join('.')
if (cache.has(key)) {
if (path !== cache.get(key)) {
ctx.addIssue({ code: 'custom', message: 'Slug already exists in ' + cache.get(key) })
}
} else {
cache.set(key, path)
}
})
// rebuild cache cause not every file is processed
// .refine(value => {
// if (cache.has(`schemas:slug:${by}:${value}`)) return false
// cache.set(`schemas:slug:${by}:${value}`, true)
// return true
// }, 'Slug already exists')
.refine(value => {
// TODO: not working in rebuild
if (cache.has(`schemas:slug:${by}:${value}`)) return false
cache.set(`schemas:slug:${by}:${value}`, true)
return true
}, 'Slug already exists')

0 comments on commit de0b6d7

Please sign in to comment.