From 1fe0bf08b1b6a2a448593ad80e87d5f53fec0992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sun, 22 May 2022 10:44:09 +0200 Subject: [PATCH] [IMP] misc: export the validate function The props validation code is actually quite difficult to get right. Also, it is sometimes useful to be able to validate an object against a specified schema. Therefore, this commit exports the standalone validate function as an utility function. --- doc/readme.md | 1 + doc/reference/props.md | 2 ++ doc/reference/utils.md | 22 ++++++++++++++++++++++ src/index.ts | 1 + 4 files changed, 26 insertions(+) diff --git a/doc/readme.md b/doc/readme.md index 90337156..36fe630f 100644 --- a/doc/readme.md +++ b/doc/readme.md @@ -45,4 +45,5 @@ Utility/helpers: - [`loadFile`](reference/utils.md#loadfile): an helper to load a file from the server - [`markup`](reference/templates.md#outputting-data): utility function to define strings that represent html (should not be escaped) - [`status`](reference/component.md#status-helper): utility function to get the status of a component (new, mounted or destroyed) +- [`validate`](reference/utils.md#validate): validates if an object satisfies a specified schema - [`whenReady`](reference/utils.md#whenready): utility function to execute code when DOM is ready diff --git a/doc/reference/props.md b/doc/reference/props.md index 50ed0b80..4d699bab 100644 --- a/doc/reference/props.md +++ b/doc/reference/props.md @@ -243,6 +243,8 @@ class ComponentB extends owl.Component { }; ``` +Note: the props validation code is done by using the [validate utility function](utils.md#validate). + ## Good Practices A `props` object is a collection of values that come from the parent. As such, diff --git a/doc/reference/utils.md b/doc/reference/utils.md index 67cee398..650769f9 100644 --- a/doc/reference/utils.md +++ b/doc/reference/utils.md @@ -8,6 +8,7 @@ functions are all available in the `owl.utils` namespace. - [`whenReady`](#whenready): executing code when DOM is ready - [`loadFile`](#loadfile): loading a file (useful for templates) - [`EventBus`](#eventbus): a simple EventBus +- [`validate`](#validate): a validation function ## `whenReady` @@ -56,3 +57,24 @@ bus.addEventListener("event", () => console.log("something happened")); bus.trigger("event"); // 'something happened' is logged ``` + +## `validate` + +The `validate` function is a function that validates if a given object satisfies a +specified schema. It is actually used by Owl itself to perform +[props validation](props.md#props-validation). For example: + +```js +validate( + { a: "hey" }, + { + id: Number, + url: [Boolean, { type: Array, element: Number }], + } +); + +// throws an error with the following information: +// - unknown key 'a', +// - 'id' is missing (should be a number), +// - 'url' is missing (should be a boolean or list of numbers), +``` diff --git a/src/index.ts b/src/index.ts index 6ab34dd9..bbc52c05 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,5 +52,6 @@ export { onWillDestroy, onError, } from "./component/lifecycle_hooks"; +export { validate } from "./validation"; export const __info__ = {};