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 8100cbe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ export interface IValidation {
size?: { max?: number; min?: number }
/** 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 }
/** Takes either a RegExp that is parsed into its source pattern and flags, or a string that reflects a JS regex and flags, which validates against a string. See JS reference for the parameters. */
regexp?: { pattern: string; flags?: string } | RegExp
/** 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
25 changes: 11 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,24 @@ const rangeForDate = () =>
max: Joi.string().optional().allow(null)
})

const regexpSchema = () =>
Joi.alternatives().try(
Joi.object().instance(RegExp),
Joi.object({
pattern: Joi.string(),
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 8100cbe

Please sign in to comment.