Skip to content
Permalink
Browse files

feat: urlSchema

  • Loading branch information...
kirillgroshkov committed May 22, 2019
1 parent 28c0835 commit 773b9ba4fb554874e286e5076cde2c35ec3644eb
Showing with 19 additions and 1 deletion.
  1. +2 −0 src/index.ts
  2. +14 −1 src/validation/joi/joi.shared.schemas.test.ts
  3. +3 −0 src/validation/joi/joi.shared.schemas.ts
@@ -46,6 +46,7 @@ import {
semVerSchema,
stringSchema,
unixTimestampSchema,
urlSchema,
userAgentSchema,
utcOffsetSchema,
verSchema,
@@ -90,6 +91,7 @@ export {
userAgentSchema,
utcOffsetSchema,
ipAddressSchema,
urlSchema,
processSharedUtil,
zipBuffer,
unzipBuffer,
@@ -1,4 +1,4 @@
import { semVerSchema } from './joi.shared.schemas'
import { semVerSchema, urlSchema } from './joi.shared.schemas'
import { validate } from './joi.validation.util'

test('semVerSchema', async () => {
@@ -16,3 +16,16 @@ test('semVerSchema', async () => {
validate(v, semVerSchema)
})
})

test('urlSchema', () => {
const schema = urlSchema()
const schemaAllowHttp = urlSchema(['https', 'http'])

expect(() => validate('abc', schema)).toThrow()

validate('https://example.com', schema)
expect(() => validate('http://example.com', schema)).toThrow()

validate('https://example.com', schemaAllowHttp)
validate('http://example.com', schemaAllowHttp)
})
@@ -4,6 +4,7 @@ import {
ArraySchemaTyped,
BooleanSchemaTyped,
ObjectSchemaTyped,
StringSchemaTyped,
} from './joi.model'

// Should all booleans be optional as a convention? So undefined will be just treated as false?
@@ -13,6 +14,8 @@ export const numberSchema = Joi.number()
export const integerSchema = Joi.number().integer()
export const dateStringSchema = stringSchema.dateString()
export const binarySchema = Joi.binary()
export const urlSchema = (scheme: string | string[] = 'https') =>
Joi.string().uri({ scheme }) as StringSchemaTyped

export function arraySchema<T> (items?: AnySchemaTyped<T, T>): ArraySchemaTyped<T> {
return items ? Joi.array().items(items) : Joi.array()

0 comments on commit 773b9ba

Please sign in to comment.
You can’t perform that action at this time.