Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Closed
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
27 changes: 25 additions & 2 deletions adonis-typings/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,12 @@ declare module '@ioc:Adonis/Core/Validator' {
* Signature to define a string or optional string type
*/
export interface StringType {
(options?: { escape?: boolean; trim?: boolean }, rules?: Rule[]): {
(options?: StringOptions, rules?: Rule[]): {
t: string
getTree(): SchemaLiteral
}
optional(
options?: { escape?: boolean; trim?: boolean },
options?: StringOptions,
rules?: Rule[]
): {
t?: string
Expand Down Expand Up @@ -577,6 +577,29 @@ declare module '@ioc:Adonis/Core/Validator' {
stripWWW?: boolean
}

/**
* String validation options
*/
export type StringOptions = {
case?:
| 'lowerCase'
| 'upperCase'
| 'camelCase'
| 'snakeCase'
| 'dashCase'
| 'pascalCase'
| 'capitalCase'
| 'sentenceCase'
| 'dotCase'
| 'titleCase'
| 'noCase'
pluralize?: boolean
singularize?: boolean
condenseWhitespace?: boolean
escape?: boolean
trim?: boolean
}

/**
* List of available validation rules. The rules are not the validation
* implementations, but instead a pointer to the validation implementation
Expand Down
8 changes: 3 additions & 5 deletions src/Schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,16 @@ import {
BooleanType,
TypedSchema,
ParsedSchemaTree,
StringOptions,
} from '@ioc:Adonis/Core/Validator'

/**
* String schema type
*/
function string(options?: { escape?: boolean; trim?: boolean }, rules?: Rule[]) {
function string(options?: StringOptions, rules?: Rule[]) {
return getLiteralType('string', false, options, rules || []) as ReturnType<StringType>
}
string.optional = function optionalString(
options?: { escape?: boolean; trim?: boolean },
rules?: Rule[]
) {
string.optional = function optionalString(options?: StringOptions, rules?: Rule[]) {
return getLiteralType('string', true, options, rules || []) as ReturnType<StringType['optional']>
}

Expand Down
74 changes: 72 additions & 2 deletions src/Validations/primitives/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/

import escape from 'validator/lib/escape'
import { SyncValidation } from '@ioc:Adonis/Core/Validator'
import { string as stringHelpers } from '@poppinss/utils/build/helpers'
import { StringOptions, SyncValidation } from '@ioc:Adonis/Core/Validator'
import { wrapCompile } from '../../Validator/helpers'

const DEFAULT_MESSAGE = 'string validation failed'
Expand All @@ -18,10 +19,14 @@ const RULE_NAME = 'string'
* Ensure value is a valid string
* @type {SyncValidation}
*/
export const string: SyncValidation<{ escape: boolean; trim: boolean }> = {
export const string: SyncValidation<StringOptions> = {
compile: wrapCompile(RULE_NAME, [], ([options]) => {
return {
compiledOptions: {
case: options && options.case,
singularize: !!(options && options.singularize),
pluralize: !!(options && options.pluralize),
condenseWhitespace: !!(options && options.condenseWhitespace),
escape: !!(options && options.escape),
trim: !!(options && options.trim),
},
Expand All @@ -35,6 +40,71 @@ export const string: SyncValidation<{ escape: boolean; trim: boolean }> = {

let mutated = false

/**
* Change string case
*/
if (compiledOptions.case) {
/**
* Using poppinss.utils
*/
if (
[
'camelCase',
'snakeCase',
'dashCase',
'pascalCase',
'capitalCase',
'sentenceCase',
'dotCase',
'titleCase',
'noCase',
].includes(compiledOptions.case)
) {
mutated = true
value = stringHelpers[compiledOptions.case](value)
}

/**
* To lowerCase
*/
if (compiledOptions.case === 'lowerCase') {
mutated = true
value = value.toLowerCase()
}

/**
* To upperCase
*/
if (compiledOptions.case === 'upperCase') {
mutated = true
value = value.toUpperCase()
}
}

/**
* Pluralize string
*/
if (compiledOptions.pluralize) {
mutated = true
value = stringHelpers.pluralize(value)
}

/**
* Singularize string
*/
if (compiledOptions.singularize) {
mutated = true
value = stringHelpers.singularize(value)
}

/**
* Condense white space
*/
if (compiledOptions.condenseWhitespace) {
mutated = true
value = stringHelpers.condenseWhitespace(value)
}

/**
* Escape string
*/
Expand Down
108 changes: 96 additions & 12 deletions test/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ test.group('Schema | String', () => {
name: 'string',
allowUndefineds: false,
async: false,
compiledOptions: { escape: false, trim: false },
compiledOptions: {
escape: false,
trim: false,
case: undefined,
pluralize: false,
singularize: false,
condenseWhitespace: false,
},
},
],
},
Expand All @@ -54,7 +61,14 @@ test.group('Schema | String', () => {
name: 'string',
allowUndefineds: false,
async: false,
compiledOptions: { escape: false, trim: false },
compiledOptions: {
escape: false,
trim: false,
case: undefined,
pluralize: false,
singularize: false,
condenseWhitespace: false,
},
},
],
},
Expand Down Expand Up @@ -82,7 +96,14 @@ test.group('Schema | String', () => {
name: 'string',
allowUndefineds: false,
async: false,
compiledOptions: { escape: false, trim: false },
compiledOptions: {
escape: false,
trim: false,
case: undefined,
pluralize: false,
singularize: false,
condenseWhitespace: false,
},
},
{
name: 'alpha',
Expand Down Expand Up @@ -118,7 +139,14 @@ test.group('Schema | String', () => {
name: 'string',
allowUndefineds: false,
async: false,
compiledOptions: { escape: true, trim: false },
compiledOptions: {
escape: true,
trim: false,
case: undefined,
pluralize: false,
singularize: false,
condenseWhitespace: false,
},
},
],
},
Expand Down Expand Up @@ -146,7 +174,14 @@ test.group('Schema | String', () => {
name: 'string',
allowUndefineds: false,
async: false,
compiledOptions: { escape: true, trim: true },
compiledOptions: {
escape: true,
trim: true,
case: undefined,
pluralize: false,
singularize: false,
condenseWhitespace: false,
},
},
],
},
Expand Down Expand Up @@ -543,7 +578,14 @@ test.group('Schema | Object', () => {
name: 'string',
allowUndefineds: false,
async: false,
compiledOptions: { escape: false, trim: false },
compiledOptions: {
escape: false,
trim: false,
case: undefined,
pluralize: false,
singularize: false,
condenseWhitespace: false,
},
},
],
},
Expand Down Expand Up @@ -586,7 +628,14 @@ test.group('Schema | Object', () => {
name: 'string',
allowUndefineds: false,
async: false,
compiledOptions: { escape: false, trim: false },
compiledOptions: {
escape: false,
trim: false,
case: undefined,
pluralize: false,
singularize: false,
condenseWhitespace: false,
},
},
],
},
Expand Down Expand Up @@ -682,7 +731,14 @@ test.group('Schema | Array', () => {
name: 'string',
allowUndefineds: false,
async: false,
compiledOptions: { escape: false, trim: false },
compiledOptions: {
escape: false,
trim: false,
case: undefined,
pluralize: false,
singularize: false,
condenseWhitespace: false,
},
},
],
},
Expand Down Expand Up @@ -744,7 +800,14 @@ test.group('Schema | Array', () => {
name: 'string',
allowUndefineds: false,
async: false,
compiledOptions: { escape: false, trim: false },
compiledOptions: {
escape: false,
trim: false,
case: undefined,
pluralize: false,
singularize: false,
condenseWhitespace: false,
},
},
],
},
Expand Down Expand Up @@ -818,7 +881,14 @@ test.group('Schema | Array', () => {
name: 'string',
allowUndefineds: false,
async: false,
compiledOptions: { escape: false, trim: false },
compiledOptions: {
escape: false,
trim: false,
case: undefined,
pluralize: false,
singularize: false,
condenseWhitespace: false,
},
},
],
},
Expand Down Expand Up @@ -913,7 +983,14 @@ test.group('Schema | Array', () => {
name: 'string',
allowUndefineds: false,
async: false,
compiledOptions: { escape: false, trim: false },
compiledOptions: {
escape: false,
trim: false,
case: undefined,
pluralize: false,
singularize: false,
condenseWhitespace: false,
},
},
],
},
Expand Down Expand Up @@ -952,7 +1029,14 @@ test.group('Schema | Array', () => {
name: 'string',
allowUndefineds: false,
async: false,
compiledOptions: { escape: false, trim: false },
compiledOptions: {
escape: false,
trim: false,
case: undefined,
pluralize: false,
singularize: false,
condenseWhitespace: false,
},
},
],
},
Expand Down
Loading