Skip to content

Commit

Permalink
feat: allow validation regexp and prohibitRegexp to be an actual RegE…
Browse files Browse the repository at this point in the history
…xp []
  • Loading branch information
jjolton-contentful committed May 10, 2024
1 parent 3d14a21 commit eea81d1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export interface IValidation {
/** Takes min and/or max parameters and validates the range of a value. */
range?: { max?: number; min?: number }
/** Takes a string that reflects a JS regex and flags, validates against a string. See JS reference for the parameters. */
regexp?: { pattern: string; flags?: string }
regexp?: { pattern: string | RegExp; flags?: string }
/** Validates that there are no other entries that have the same field value at the time of publication. */
unique?: true
/** Validates that a value falls within a certain range of dates. */
Expand Down
24 changes: 10 additions & 14 deletions src/lib/offline-api/validator/schema/field-validations-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,23 @@ const rangeForDate = () =>
max: Joi.string().optional().allow(null)
})

const stringOrRegExp = () => Joi.alternatives().try(Joi.string(), Joi.object().instance(RegExp))

const regexpSchema = () =>
Joi.object({
pattern: stringOrRegExp(),
flags: Joi.string().allow(null).optional()
})

const linkContentType = validation('linkContentType', Joi.array().items(Joi.string()))
const inValidation = validation('in', Joi.array())
const linkMimetypeGroup = validation('linkMimetypeGroup', Joi.array().items(Joi.string()))
const size = validation('size', range('number'))
const rangeValidation = validation('range', range('number'))

const regexp = validation(
'regexp',
Joi.object({
pattern: Joi.string(),
flags: Joi.string().allow(null).optional()
})
)
const regexp = validation('regexp', regexpSchema())

const prohibitRegexp = validation(
'prohibitRegexp',
Joi.object({
pattern: Joi.string(),
flags: Joi.string().allow(null).optional()
})
)
const prohibitRegexp = validation('prohibitRegexp', regexpSchema())

const unique = validation('unique', Joi.boolean())
const dateRange = validation('dateRange', rangeForDate())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ describe('payload validation', function () {
expect(errors).to.eql([[]])
})

it('allows RegExp for pattern', async function () {
const errors = await validateBatches(function up(migration) {
const person = migration.createContentType('person').name('Person').description('A Person')

person
.createField('fullName')
.name('Full Name')
.type('Symbol')
.validations([{ regexp: { pattern: /^[A-Za-zs]+$/, flags: null } }])
}, [])

expect(errors).to.eql([[]])
})

it('can validate all blocks and inlines for RichText', async function () {
const errors = await validateBatches(function up(migration) {
const novel = migration
Expand Down

0 comments on commit eea81d1

Please sign in to comment.