Skip to content

Commit

Permalink
[UPDATE] better syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mathix420 committed May 30, 2022
1 parent 4521c98 commit 8f91632
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/vuito/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export class Template {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
// Map each fields checker in template
Object.entries(template as VTemplate).map(([key, tests]) => {
for (const [key, tests] of Object.entries(template as VTemplate)) {
// Add `check` method to all fields checkers
tests.check = function (value, parent) {
(tests as VTemplateRow).check = function (value, parent) {
return new Promise(async (resolve, reject) => {
await this.map(({ message, test, onlyIf }) => {
for (const { message, test, onlyIf } of this) {
// Conditional check
if (onlyIf && parent && !onlyIf(parent)) return;

Expand All @@ -21,28 +21,28 @@ export class Template {

// If test fail => we reject with error message
if (!test(value)) reject(message);
});
}
// All tests passed
resolve();
});
};

self[key] = tests;
});
self[key] = tests as VTemplateRow;
}
}

/**
* Global checker
*/
public check(object: Record<string, unknown>): Promise<void> {
return new Promise(async (resolve, reject) => {
await Object.entries(this).map(([key, tests]) => {
for (const [key, tests] of Object.entries(this)) {
// Skip `check` method in the template
// if (key === 'check') return;

// Perform all sub tests, if any fail, the exit with the error message
(tests as VTemplateRow).check(object[key], object).catch(reject);
});
}

// All tests passed
resolve();
Expand Down

0 comments on commit 8f91632

Please sign in to comment.