Skip to content
Merged
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,27 @@ const schema = {
}
```

### Nested objects

```js
const schema = {
dot: {
$$type: "object",
x: "number", // object props here
y: "number", // object props here
},
circle: {
$$type: "object|optional", // using other shorthands
o: {
$$type: "object",
x: "number",
y: "number",
},
r: "number"
}
};
```

# Alias definition
You can define custom aliases.

Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ declare module "fastest-validator" {
* @return {ValidationRule}
*/
getRuleFromSchema(
name: ValidationRuleName | ValidationRuleName[]
name: ValidationRuleName | ValidationRuleName[] | { [key: string]: unknown }
): {
messages: MessagesType;
schema: ValidationSchema;
Expand Down
14 changes: 14 additions & 0 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,20 @@ class Validator {
schema.optional = true;
}

if (schema.$$type) {
const type = schema.$$type;
const otherShorthandProps = this.getRuleFromSchema(type).schema;
delete schema.$$type;
const props = Object.assign({}, schema);

for (const key in schema) { // clear object without changing reference
delete schema[key];
}

deepExtend(schema, otherShorthandProps, { skipIfExist: true });
schema.props = props;
}

const alias = this.aliases[schema.type];
if (alias) {
delete schema.type;
Expand Down
18 changes: 18 additions & 0 deletions test/typescript/validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@ describe('TypeScript Definitions', () => {
});

});

describe("Test object shorthand rule ($$type)", () => {
it("should convert", () => {
const res = v.getRuleFromSchema({
$$type: "object",
name: { type: "string" },
age: { type: "number" }
});

expect(res.schema).toEqual({
type: "object" ,
props: {
name: { type: "string" },
age: { type: "number" }
}
});
});
});
});

describe('Test makeError', () => {
Expand Down
81 changes: 80 additions & 1 deletion test/validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe("Test getRuleFromSchema method", () => {
}).toThrowError("Invalid 's' type in validator schema.");
});

describe("Test string shorthard rules", () => {
describe("Test string shorthand rules", () => {

it("should convert only type", () => {
const res = v.getRuleFromSchema("string");
Expand All @@ -202,6 +202,41 @@ describe("Test getRuleFromSchema method", () => {
});

});

describe("Test objects shorthand rule ($$type)", () => {
it("should convert", () => {
const res = v.getRuleFromSchema({
$$type: "object",
name: { type: "string" },
age: { type: "number" }
});

expect(res.schema).toEqual({
type: "object" ,
props: {
name: { type: "string" },
age: { type: "number" }
}
});
});

it("should work with other shorthand rules", () => {
const res = v.getRuleFromSchema({
$$type: "object|optional",
name: { type: "string" },
age: { type: "number" }
});

expect(res.schema).toEqual({
type: "object" ,
optional: true,
props: {
name: { type: "string" },
age: { type: "number" }
}
});
});
});
});

describe("Test makeError", () => {
Expand Down Expand Up @@ -496,3 +531,47 @@ describe("Test default settings", () => {
});
});
});

describe("Test objects shorthand", () => {
const v = new Validator();

it("should work with nested objects", () => {
const check = v.compile({
dot: {
$$type: "object",
x: "number",
y: "number",
},
circle: {
$$type: "object",
o: {
$$type: "object",
x: "number",
y: "number",
},
r: "number"
}
});

expect(
check({
dot: { x: 10, y: 3 },
circle: {
o: { x: 10, y: 3 },
r: 30
}
})
).toBe(true);

expect(
check({
dot: { x: 10, y: 3 },
circle: {
o: { x: 10, y: "s" },
r: 30
}
})
).toEqual(expect.any(Array));
});
});