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

🎨 add warning for http and not valid slug #2496 #2503

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion sanityv3/schemas/editors/blockContentType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Flags } from '../../src/lib/datasetHelpers'
import { ExternalLinkRenderer, SubScriptRenderer, SuperScriptRenderer } from '../components'
import routes from '../routes'
import { defaultColors } from '../defaultColors'
import { strictExternal, warnHttpExternal, warnHttpOrNotValidSlugExternal } from '../validations/validateSlug'

export type BlockContentProps = {
h2?: boolean
Expand Down Expand Up @@ -194,7 +195,12 @@ export const configureBlockContent = (options: BlockContentProps = {}): BlockDef
{
name: 'href',
type: 'url',
validation: (Rule: any) => Rule.uri({ scheme: ['http', 'https', 'tel', 'mailto'] }),
validation: (Rule: any) =>
Rule.uri({ scheme: ['http', 'https', 'tel', 'mailto'] })
.custom((value: any, context: ValidationContext) => {
return warnHttpOrNotValidSlugExternal(value, context)
})
.warning(),
},
],
}
Expand Down
9 changes: 8 additions & 1 deletion sanityv3/schemas/objects/linkSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AnchorLinkDescription } from './anchorReferenceField'
// eslint-disable-next-line import/no-unresolved
import { defaultLanguage } from '../../languages'
import { apiVersion } from '../../sanity.client'
import { warnHttpOrNotValidSlugExternal } from '../validations/validateSlug'

export type ReferenceTarget = {
type: string
Expand Down Expand Up @@ -118,7 +119,13 @@ export const getLinkSelectorFields = (labelFieldset?: string, flag?: string) =>
const { parent } = context as { parent: LinkSelector }
if (isHidden(parent)) return true
const connectedField = parent?.linkToOtherLanguage ? parent?.referenceToOtherLanguage : parent?.reference
return validateInternalOrExternalUrl(value, connectedField)
const internalValidation = validateInternalOrExternalUrl(value, connectedField)
//If message from internalValidation return that if not continue to check external url
if (internalValidation) {
return warnHttpOrNotValidSlugExternal(value, context)
} else {
return internalValidation
}
}),
hidden: ({ parent }: { parent: LinkSelector }) => isHidden(parent),
},
Expand Down
15 changes: 15 additions & 0 deletions sanityv3/schemas/validations/validateSlug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,18 @@ export const withSlugValidation = (options: any) => {
}
: options
}

const stringIsSlug = /^[a-z0-9]+(-[a-z0-9]+)*$/
const httpRegex = /^(?:http:\/\/|).*$/
export const warnHttpOrNotValidSlugExternal = (slug: string, context: ValidationContext) => {
const isHttp = httpRegex.test(slug)
const validSlug = stringIsSlug.test(slug)
let message = ''
if (isHttp) {
message = 'Use https in url. '
}
if (!validSlug) {
message = message.concat(`Should only contain lowercase letters [a-z], numbers [0-9], and dashes ("-")`)
}
return isHttp || !validSlug ? message : true
}
Loading