Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added normalize transform option to normalize string spaces #217

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ A standalone string cannot be modified, i.e. `data = 'a'; ajv.validate(schema, d
- `toLowerCase`: convert to lower case
- `toUpperCase`: convert to upper case
- `toEnumCase`: change string case to be equal to one of `enum` values in the schema
- `normalizeSpaces`: replace all multiple white spaces with a single white space in a string

Transformations are applied in the order they are listed.

Expand Down
37 changes: 37 additions & 0 deletions spec/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,43 @@ describe('keyword "transform"', () => {
})

ajvs.forEach((ajv, i) => {
it(`should transform normalizeSpaces #${i}`, () => {
let schema, data

data = [" normalize object to test "]
schema = {type: "array", items: {type: "string", transform: ["normalizeSpaces"]}}
ajv.validate(schema, data).should.equal(true)
data.should.deep.equal([" normalize object to test "])

data = ["normalize"]
schema = {type: "array", items: {type: "string", transform: ["normalizeSpaces"]}}
ajv.validate(schema, data).should.equal(true)
data.should.deep.equal(["normalize"])

data = ["normalize object to test"]
schema = {type: "array", items: {type: "string", transform: ["normalizeSpaces"]}}
ajv.validate(schema, data).should.equal(true)
data.should.deep.equal(["normalize object to test"])

data = ["normalize object to test"]
schema = {type: "array", items: {type: "string", transform: ["normalizeSpaces"]}}
ajv.validate(schema, data).should.equal(true)
data.should.deep.equal(["normalize object to test"])

data = [
`A tab\t\tanother multiple tabs\t\t\t\tnew line
multiple new lines



Multiple spaces end`,
]
schema = {type: "array", items: {type: "string", transform: ["normalizeSpaces"]}}
ajv.validate(schema, data).should.equal(true)
data.should.deep.equal([
"A tab another multiple tabs new line multiple new lines Multiple spaces end",
])

it(`shouldn't mutate the transform array of the schema while compiling it #${i}`, () => {
const data = {p: " trimObject "}
const schema = {type: "object", properties: {p: {type: "string", transform: ["trimLeft"]}}}
Expand Down
2 changes: 2 additions & 0 deletions src/definitions/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type TransformName =
| "toLowerCase"
| "toUpperCase"
| "toEnumCase"
| "normalizeSpaces"

interface TransformConfig {
hash: Record<string, string | undefined>
Expand All @@ -26,6 +27,7 @@ const transform: {[key in TransformName]: Transform} = {
toLowerCase: (s) => s.toLowerCase(),
toUpperCase: (s) => s.toUpperCase(),
toEnumCase: (s, cfg) => cfg?.hash[configKey(s)] || s,
normalizeSpaces: (s) => s.replace(/[\s\t\n]+/g, " "),
}

const getDef: (() => CodeKeywordDefinition) & {
Expand Down