Skip to content

Commit

Permalink
🎨 add warning for http and not valid slug #2496 (#2503)
Browse files Browse the repository at this point in the history
* 🎨 add warning for http and not valid slug

* πŸ› Refactor after comments #2496

* 🚨 Lint #2496

* πŸ’š πŸ› Fix import bug #2496

* πŸ› Fix error with validation #2496

* πŸ› Check lowercase excluding queryparam #2496

* πŸ› Allow http to avoid error msg that confuses #2496

---------

Co-authored-by: Padmaja <[email protected]>
  • Loading branch information
BorghildSelle and padms authored Nov 8, 2024
1 parent 08d9e93 commit d677f1e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
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 @@ -181,7 +182,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)
}

0 comments on commit d677f1e

Please sign in to comment.