Skip to content

Commit 0f4a7b5

Browse files
authored
Merge pull request #180 from icebob/single-line-string
[feat] single line string validation
2 parents 8a91760 + 167590a commit 0f4a7b5

File tree

6 files changed

+43
-1
lines changed

6 files changed

+43
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ Property | Default | Description
917917
`alphanum` | `null` | The value must be an alphanumeric string.
918918
`alphadash` | `null` | The value must be an alphabetic string that contains dashes.
919919
`hex` | `null` | The value must be a hex string.
920+
`singleLine` | `null` | The value must be a single line string.
920921
`trim` | `null` | If `true`, the value will be trimmed. _It's a sanitizer, it will change the value in the original object._
921922
`trimLeft` | `null` | If `true`, the value will be left trimmed. _It's a sanitizer, it will change the value in the original object._
922923
`trimRight` | `null` | If `true`, the value will be right trimmed. _It's a sanitizer, it will change the value in the original object._
@@ -1296,6 +1297,7 @@ Name | Default text
12961297
`stringAlphanum` | The '{field}' field must be an alphanumeric string.
12971298
`stringAlphadash` | The '{field}' field must be an alphadash string.
12981299
`stringHex` | The '{field}' field must be a hex string.
1300+
`stringSingleLine` | The '{field}' field must be a single line string.
12991301
`number` | The '{field}' field must be a number.
13001302
`numberMin` | The '{field}' field must be greater than or equal to {expected}.
13011303
`numberMax` | The '{field}' field must be less than or equal to {expected}.

index.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,15 @@ declare module "fastest-validator" {
401401
*/
402402
alphadash?: boolean;
403403
/**
404-
* The value must be a hex string that contains dashes
404+
* The value must be a hex string
405+
* @default false
405406
*/
406407
hex?: boolean;
408+
/**
409+
* The value must be a singleLine string
410+
* @default false
411+
*/
412+
singleLine?: boolean;
407413
/**
408414
* if true and the type is not a String, converts with String()
409415
* @default false

lib/messages.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
stringAlphanum: "The '{field}' field must be an alphanumeric string.",
1717
stringAlphadash: "The '{field}' field must be an alphadash string.",
1818
stringHex: "The '{field}' field must be a hex string.",
19+
stringSingleLine: "The '{field}' field must be a single line string.",
1920

2021
number: "The '{field}' field must be a number.",
2122
numberMin: "The '{field}' field must be greater than or equal to {expected}.",

lib/rules/string.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ module.exports = function checkString({ schema, messages }, path, context) {
207207
`);
208208
}
209209

210+
if(schema.singleLine === true) {
211+
src.push(`
212+
if(value.includes("\\n")) {
213+
${this.makeError({ type: "stringSingleLine", messages })}
214+
}
215+
`);
216+
}
217+
210218
src.push(`
211219
return value;
212220
`);

test/rules/string.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ describe("Test rule: string", () => {
178178
expect(check("0123456789abcDEF")).toEqual(true);
179179
});
180180

181+
it("check singleLine string", () => {
182+
const check = v.compile({ $$root: true, type: "string", singleLine: true});
183+
const message = "The '' field must be a single line string.";
184+
185+
expect(check("abc")).toEqual(true);
186+
expect(check("abc\n")).toEqual([{type: "stringSingleLine", message }]);
187+
expect(check(`
188+
abc
189+
def
190+
`)).toEqual([{type: "stringSingleLine", message }]);
191+
});
192+
181193
it("should convert & check values", () => {
182194
const check = v.compile({ $$root: true, type: "string", convert: true });
183195
expect(check("")).toEqual(true);

test/typescript/rules/string.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ describe('TypeScript Definitions', () => {
181181
expect(check(new Date())).toEqual(true);
182182
});
183183

184+
it("check singleLine string", () => {
185+
const schema: RuleString = { $$root: true, type: "string", singleLine: true }
186+
const check = v.compile(schema);
187+
const message = "The '' field must be a single line string.";
188+
189+
expect(check("abc")).toEqual(true);
190+
expect(check("abc\n")).toEqual([{type: "stringSingleLine", message }]);
191+
expect(check(`
192+
abc
193+
def
194+
`)).toEqual([{type: "stringSingleLine", message }]);
195+
});
196+
184197
describe('Test sanitization', () => {
185198

186199
it('should convert & check values', () => {

0 commit comments

Comments
 (0)