Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 actual RegExp', 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: /^[A-Za-zs]+$/g }])
}, [])

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