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 all commits
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)
})
.error(),
},
],
}
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
5 changes: 5 additions & 0 deletions sanityv3/schemas/validations/checkEquinorUrls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { getAllDomainUrls } from '../../../satellitesConfig'

export const isEquinorUrl = (slug: string) => {
return getAllDomainUrls().some((allowedUrl) => slug.toLowerCase().startsWith(allowedUrl))
}
26 changes: 26 additions & 0 deletions sanityv3/schemas/validations/validateSlug.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ValidationContext } from 'sanity'
import { apiVersion } from '../../sanity.client'
import { Flags } from '../../src/lib/datasetHelpers'
import { isEquinorUrl } from './checkEquinorUrls'

const validateIsUniqueWithinLocale = async (slug: string, context: ValidationContext) => {
const { document, getClient } = context
Expand Down Expand Up @@ -41,3 +42,28 @@ export const withSlugValidation = (options: any) => {
}
: options
}

const stringIsSlug =
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/
const httpRegex = /^http:\/\/[^\s]+$/
export const warnHttpOrNotValidSlugExternal = (slug: string) => {
if (!slug) return true
if (!slug.startsWith('http')) return true // ignore mailto , tel
const isHttp = httpRegex.test(slug)
const validSlug = stringIsSlug.test(slug)
const slugWithOutQueryParam = slug.split('?')[0]
const isInvalidEquinorUrl = slugWithOutQueryParam !== slugWithOutQueryParam.toLowerCase() && isEquinorUrl(slug)

let message = ''
if (isHttp) {
message = 'Use https in url. '
}
if (!validSlug) {
message = message.concat(`Not a valid url.`)
}

if (isInvalidEquinorUrl) {
message = message.concat('Equinor urls should contain lowercase letters only.')
}
return isHttp || !validSlug || isInvalidEquinorUrl ? message : true
}
4 changes: 4 additions & 0 deletions satellitesConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,7 @@ export const getDomain = (dataset) => websiteDomains[dataset]?.url || 'Domain no
export const getMetaTitleSuffix = (dataset) => {
return websiteDomains[dataset]?.meta || 'Equinor'
}

export const getAllDomainUrls = () => {
return Object.keys(datasets).map((dataset) => websiteDomains[dataset]?.url)
}
Loading