Skip to content

Commit

Permalink
feat(validator): export schema type
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm authored and AliMD committed Jan 3, 2023
1 parent bd7391c commit da91546
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions core/validator/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export type schema = {
[key: string]: schema | 'string' | 'number' | 'boolean';
export type Schema = {
[key: string]: Schema | 'string' | 'number' | 'boolean';
}
8 changes: 4 additions & 4 deletions core/validator/src/validator.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {isNumber} from '@alwatr/math';

import type {schema} from './type.js';
import type {Schema} from './type.js';

export {validator};
export {validator, Schema};

function validator<DataType extends Record<string, unknown>>(value: Record<string, unknown>, schema: schema): DataType {
function validator<DataType extends Record<string, unknown>>(value: Record<string, unknown>, schema: Schema): DataType {
for (const paramName in schema) {
if (!Object.prototype.hasOwnProperty.call(schema, paramName)) continue;

const valueType = schema[paramName];
// nested object
if (typeof schema[paramName] === 'object') {
value[paramName] =
validator<DataType>(value[paramName] as Record<string, unknown>, schema[paramName] as schema);
validator<DataType>(value[paramName] as Record<string, unknown>, schema[paramName] as Schema);
}

const validValue = value[paramName] as string | number | boolean;
Expand Down

0 comments on commit da91546

Please sign in to comment.