Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,19 @@ check({ roles: ["user", "admin", "user"] }); // Fail
check({ roles: [1, 2, 1] }); // Fail
```

**Example for `convert`:**

```js
const schema = {
roles: { type: "array", items: 'string', convert: true }
}
const check = v.compile(schema);

check({ roles: ["user"] }); // Valid
check({ roles: "user" }); // Valid
// After both validation: roles = ["user"]
```

### Properties
Property | Default | Description
-------- | -------- | -----------
Expand All @@ -497,6 +510,7 @@ Property | Default | Description
`unique` | `null` | The array must be unique (array of objects is always unique).
`enum` | `null` | Every element must be an element of the `enum` array.
`items` | `null` | Schema for array items.
`convert`| `null` | Wrap value into array if different type provided

## `boolean`
This is a `Boolean` validator.
Expand Down
11 changes: 11 additions & 0 deletions lib/rules/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
module.exports = function ({ schema, messages }, path, context) {
const src = [];

let sanitized = false;
if (schema.convert === true) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we shouldn't have to wrap the null and undefined values because they are no "real" values.
The current implementation will wrap the { roles: null } to { roles: [null] }

sanitized = true;
src.push(`
if (!Array.isArray(value)) {
value = [value];
}
`);
}

src.push(`
if (!Array.isArray(value)) {
${this.makeError({ type: "array", actual: "value", messages })}
Expand Down Expand Up @@ -99,6 +109,7 @@ module.exports = function ({ schema, messages }, path, context) {
}

return {
sanitized,
source: src.join("\n")
};
};
5 changes: 5 additions & 0 deletions test/rules/array.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ describe("Test rule: array", () => {
expect(customFnItems).toHaveBeenNthCalledWith(1, 1, [], schema.numbers.items, "numbers[]", { numbers: [1,2] }, expect.any(Object));
expect(customFnItems).toHaveBeenNthCalledWith(2, 2, [], schema.numbers.items, "numbers[]", { numbers: [1,2] }, expect.any(Object));
});

it("should wrap single value into array", () => {
const check = v.compile({ $$root: true, type: "array", items: "string", convert: true });
expect(check("a")).toEqual(true);
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add more test, to check the santitized value as well, and add test when the input:

  • an array with value,
  • an empty array
  • null
  • undefined


describe("Test sanitization", () => {

Expand Down
5 changes: 5 additions & 0 deletions test/typescript/rules/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ describe('TypeScript Definitions', () => {
{ type: 'string', field: '[3]', actual: true, message: 'The \'[3]\' field must be a string.' },
]);
});

it("should wrap single value into array", () => {
const check = v.compile({ $$root: true, type: "array", items: "string", convert: true });
expect(check("a")).toEqual(true);
});
});

describe("Test sanitization", () => {
Expand Down