Skip to content

Commit

Permalink
[validation] Allow unicode in URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Apr 9, 2018
1 parent 63f511f commit c21ccd3
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 303 deletions.
4 changes: 1 addition & 3 deletions packages/@sanity/validation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@
"clone-deep": "^2.0.1",
"es6-error": "^4.0.2",
"throat": "^4.1.0",
"type-of-is": "^3.5.1",
"url-parse": "^1.2.0"
"type-of-is": "^3.5.1"
},
"jest": {
"verbose": true,
"testEnvironment": "node",
"collectCoverage": true
}
}
48 changes: 10 additions & 38 deletions packages/@sanity/validation/src/Rule.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const cloneDeep = require('clone-deep')
const validate = require('./validate')
const escapeRegex = require('./util/escapeRegex')
const createUriRegex = require('./util/createUriRegex')
const validate = require('./validate')

const knownTypes = ['Object', 'String', 'Number', 'Boolean', 'Array', 'Date']
const isExclusive = ['type', 'uri', 'email']
Expand Down Expand Up @@ -215,54 +214,27 @@ class Rule {
return this.cloneWithRules([{flag: 'email', constraint: options}])
}

// eslint-disable-next-line complexity
uri(opts = {}) {
const options = Object.assign(
{scheme: ['http', 'https'], allowRelative: false, relativeOnly: false},
opts
)

let customScheme = ''

if (
!(options.scheme instanceof RegExp) &&
typeof options.scheme !== 'string' &&
!Array.isArray(options.scheme)
) {
throw new Error('scheme must be a RegExp, String, or Array')
}
const allowedSchemes = Array.isArray(options.scheme) ? options.scheme : [options.scheme]
options.scheme = allowedSchemes.map(scheme => {
const schemeIsString = typeof scheme === 'string'
if (!(scheme instanceof RegExp) && schemeIsString === false) {
throw new Error('scheme must be a RegExp or a String')
}

if (!Array.isArray(options.scheme)) {
options.scheme = [options.scheme]
}
return schemeIsString ? new RegExp(`^${escapeRegex(scheme)}$`) : scheme
})

if (!options.scheme.length) {
throw new Error('scheme must have at least 1 scheme specified')
}

// Flatten the array into a string to be used to match the schemes.
for (let i = 0; i < options.scheme.length; ++i) {
const scheme = options.scheme[i]
if (!(scheme instanceof RegExp) && typeof scheme !== 'string') {
throw new Error(`scheme at position ${i} must be a RegExp or String`)
}

// Add OR separators if a value already exists
customScheme += customScheme ? '|' : ''

const schemePattern = scheme instanceof RegExp ? scheme.source : escapeRegex(scheme)

// If someone wants to match HTTP or HTTPS for example then we need to support
// both RegExp and String so we don't escape their pattern unknowingly.
if (!(scheme instanceof RegExp) && !/[a-zA-Z][a-zA-Z0-9+-\.]*/.test(scheme)) {
throw new Error(`scheme at position ${i} must be a valid scheme`)
}

customScheme += schemePattern
}

const regex = createUriRegex(customScheme, options.allowRelative, options.relativeOnly)
return this.cloneWithRules([{flag: 'uri', constraint: {options, regex}}])
return this.cloneWithRules([{flag: 'uri', constraint: {options}}])
}

// Array only
Expand Down
38 changes: 0 additions & 38 deletions packages/@sanity/validation/src/util/createUriRegex.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/@sanity/validation/src/util/escapeRegex.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-useless-escape */
module.exports = string => {
// Escape ^$.*+-?=!:|\/()[]{},
return string.replace(/[\^\$\.\*\+\-\?\=\!\:\|\\\/\(\)\[\]\{\}\,]/g, '\\$&')
Expand Down
208 changes: 0 additions & 208 deletions packages/@sanity/validation/src/util/rfc3986.js

This file was deleted.

0 comments on commit c21ccd3

Please sign in to comment.