mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[IMP] runtime: allow validating object values using a type description
In JS, objects are often used as a mapping that gives fast lookup on the keys, in those cases, the keys themselves are not known in advance, but the values may have a shape that they're expected to follow. This commit adds support for a `values` key in schema for objects, which will be checked against all values in the object during validation.
This commit is contained in:
committed by
Géry Debongnie
parent
fea8fcaf61
commit
47009912df
@@ -404,7 +404,7 @@ describe("props validation", () => {
|
||||
await mountProm;
|
||||
expect(error!).toBeDefined();
|
||||
expect(error!.message).toBe(
|
||||
"Invalid props for component 'SubComp': 'p' has not the correct shape (unknown key 'extra')"
|
||||
"Invalid props for component 'SubComp': 'p' doesn't have the correct shape (unknown key 'extra')"
|
||||
);
|
||||
props = { p: { id: "1", url: "url" } };
|
||||
app = new App(Parent, { test: true });
|
||||
@@ -413,7 +413,7 @@ describe("props validation", () => {
|
||||
await mountProm;
|
||||
expect(error!).toBeDefined();
|
||||
expect(error!.message).toBe(
|
||||
"Invalid props for component 'SubComp': 'p' has not the correct shape ('id' is not a number)"
|
||||
"Invalid props for component 'SubComp': 'p' doesn't have the correct shape ('id' is not a number)"
|
||||
);
|
||||
error = undefined;
|
||||
props = { p: { id: 1 } };
|
||||
@@ -423,7 +423,7 @@ describe("props validation", () => {
|
||||
await mountProm;
|
||||
expect(error!).toBeDefined();
|
||||
expect(error!.message).toBe(
|
||||
"Invalid props for component 'SubComp': 'p' has not the correct shape ('url' is missing (should be a string))"
|
||||
"Invalid props for component 'SubComp': 'p' doesn't have the correct shape ('url' is missing (should be a string))"
|
||||
);
|
||||
});
|
||||
|
||||
@@ -470,7 +470,7 @@ describe("props validation", () => {
|
||||
await mountProm;
|
||||
expect(error!).toBeDefined();
|
||||
expect(error!.message).toBe(
|
||||
"Invalid props for component 'SubComp': 'p' has not the correct shape ('url' is not a boolean or list of numbers)"
|
||||
"Invalid props for component 'SubComp': 'p' doesn't have the correct shape ('url' is not a boolean or list of numbers)"
|
||||
);
|
||||
});
|
||||
|
||||
@@ -502,7 +502,7 @@ describe("props validation", () => {
|
||||
}
|
||||
expect(error!).toBeDefined();
|
||||
expect(error!.message).toBe(
|
||||
"Invalid props for component 'TestComponent': 'myprop[0]' has not the correct shape (unknown key 'a')"
|
||||
"Invalid props for component 'TestComponent': 'myprop[0]' doesn't have the correct shape (unknown key 'a')"
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -145,17 +145,51 @@ describe("validateSchema", () => {
|
||||
const schema: Schema = { p: { type: Object, shape: { id: Number, url: String } } };
|
||||
expect(validateSchema({ p: [] }, schema)).toEqual(["'p' is not an object"]);
|
||||
expect(validateSchema({ p: {} }, schema)).toEqual([
|
||||
"'p' has not the correct shape ('id' is missing (should be a number), 'url' is missing (should be a string))",
|
||||
"'p' doesn't have the correct shape ('id' is missing (should be a number), 'url' is missing (should be a string))",
|
||||
]);
|
||||
expect(validateSchema({ p: { id: 1, url: "asf" } }, schema)).toEqual([]);
|
||||
expect(validateSchema({ p: { id: 1, url: 1 } }, schema)).toEqual([
|
||||
"'p' has not the correct shape ('url' is not a string)",
|
||||
"'p' doesn't have the correct shape ('url' is not a string)",
|
||||
]);
|
||||
expect(validateSchema({ p: undefined }, schema)).toEqual([
|
||||
"'p' is undefined (should be a object)",
|
||||
]);
|
||||
});
|
||||
|
||||
test("objects with a values schema", () => {
|
||||
const schema: Schema = {
|
||||
p: { type: Object, values: { type: Object, shape: { id: Number, url: String } } },
|
||||
};
|
||||
expect(validateSchema({ p: [] }, schema)).toEqual(["'p' is not an object"]);
|
||||
expect(validateSchema({ p: {} }, schema)).toEqual([]);
|
||||
expect(validateSchema({ p: { id: 1, url: "asf" } }, schema)).toEqual([
|
||||
"some of the values in 'p' are invalid ('id' is not an object, 'url' is not an object)",
|
||||
]);
|
||||
expect(
|
||||
validateSchema(
|
||||
{
|
||||
p: {
|
||||
a: { id: 1, url: "asf" },
|
||||
},
|
||||
},
|
||||
schema
|
||||
)
|
||||
).toEqual([]);
|
||||
expect(
|
||||
validateSchema(
|
||||
{
|
||||
p: {
|
||||
a: { id: 1, url: "asf" },
|
||||
b: { id: 1, url: 1 },
|
||||
},
|
||||
},
|
||||
schema
|
||||
)
|
||||
).toEqual([
|
||||
"some of the values in 'p' are invalid ('b' doesn't have the correct shape ('url' is not a string))",
|
||||
]);
|
||||
});
|
||||
|
||||
test("objects with more complex shape", () => {
|
||||
const schema: Schema = {
|
||||
p: {
|
||||
@@ -168,10 +202,10 @@ describe("validateSchema", () => {
|
||||
};
|
||||
expect(validateSchema({ p: [] }, schema)).toEqual(["'p' is not an object"]);
|
||||
expect(validateSchema({ p: {} }, schema)).toEqual([
|
||||
"'p' has not the correct shape ('id' is missing (should be a number), 'url' is missing (should be a boolean or list of numbers))",
|
||||
"'p' doesn't have the correct shape ('id' is missing (should be a number), 'url' is missing (should be a boolean or list of numbers))",
|
||||
]);
|
||||
expect(validateSchema({ p: { id: 1, url: "asf" } }, schema)).toEqual([
|
||||
"'p' has not the correct shape ('url' is not a boolean or list of numbers)",
|
||||
"'p' doesn't have the correct shape ('url' is not a boolean or list of numbers)",
|
||||
]);
|
||||
expect(validateSchema({ p: { id: 1, url: true } }, schema)).toEqual([]);
|
||||
expect(validateSchema({ p: undefined }, schema)).toEqual([
|
||||
@@ -183,11 +217,11 @@ describe("validateSchema", () => {
|
||||
const schema: Schema = { p: { type: Object, shape: { id: Number, "*": true } } };
|
||||
expect(validateSchema({ p: [] }, schema)).toEqual(["'p' is not an object"]);
|
||||
expect(validateSchema({ p: {} }, schema)).toEqual([
|
||||
"'p' has not the correct shape ('id' is missing (should be a number))",
|
||||
"'p' doesn't have the correct shape ('id' is missing (should be a number))",
|
||||
]);
|
||||
expect(validateSchema({ p: { id: 1 } }, schema)).toEqual([]);
|
||||
expect(validateSchema({ p: { id: "asdf" } }, schema)).toEqual([
|
||||
"'p' has not the correct shape ('id' is not a number)",
|
||||
"'p' doesn't have the correct shape ('id' is not a number)",
|
||||
]);
|
||||
expect(validateSchema({ p: { id: 1, url: 1 } }, schema)).toEqual([]);
|
||||
expect(validateSchema({ p: undefined }, schema)).toEqual([
|
||||
@@ -222,7 +256,7 @@ describe("validateSchema", () => {
|
||||
expect(validateSchema({ p: [{}] }, schema)).toEqual([]);
|
||||
expect(validateSchema({ p: [{ num: 1 }] }, schema)).toEqual([]);
|
||||
expect(validateSchema({ p: [{ num: true }] }, schema)).toEqual([
|
||||
"'p[0]' has not the correct shape ('num' is not a number)",
|
||||
"'p[0]' doesn't have the correct shape ('num' is not a number)",
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user