From 01f33d8bb2d97e818b36436f5ba672ee98d981b7 Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Mon, 26 Aug 2024 13:41:20 +0200 Subject: [PATCH 1/2] feat: int() --- src/common/schema/schema.spec.ts | 5 +++-- src/common/schema/schema.ts | 15 ++++++++------- src/common/schema/types.ts | 1 + 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/common/schema/schema.spec.ts b/src/common/schema/schema.spec.ts index 35b4e7b..b06445c 100644 --- a/src/common/schema/schema.spec.ts +++ b/src/common/schema/schema.spec.ts @@ -1,5 +1,5 @@ import { cloneJsonObject } from '../data' -import { boolean, literal, number, object, string, stringLiterals, union } from './schema' +import { boolean, int, literal, number, object, string, stringLiterals, union } from './schema' import type { Infer } from './types' describe('schema', () => { @@ -9,7 +9,7 @@ describe('schema', () => { const schema = object({ id: string().default(() => '123'), name: string(), - age: number().optional(), + age: int().optional(), active: boolean(), // status: stringLiterals(['active', 'trialing', 'past_due', 'paused', 'deleted']), status: string(), @@ -36,6 +36,7 @@ describe('schema', () => { "type": "boolean", }, "age": Object { + "_integer": true, "_optional": true, "type": "number", }, diff --git a/src/common/schema/schema.ts b/src/common/schema/schema.ts index 730c4d4..7793514 100644 --- a/src/common/schema/schema.ts +++ b/src/common/schema/schema.ts @@ -1,4 +1,4 @@ -import { first, isBoolean, isFunction, isNumber, isObject, isString } from '../data' +import { first, isBoolean, isFunction, isInteger, isNumber, isObject, isString } from '../data' import type { SchemaDefinitionObject, Type, TypeNames, TypeObject } from './types' // Helper @@ -64,6 +64,13 @@ export function number() { }) } +export function int() { + return generic('number', { + _integer: true, + _check: isInteger, + }) +} + export function boolean() { return generic('boolean', { _check: isBoolean, @@ -94,12 +101,6 @@ export function object(tobj: T): TypeObject const result = fn.call(this as any, obj, this as any) if (result !== undefined) return result - - // if (!isObject(obj)) - // return new Error('expected object input') - // if (!isObject(this._object)) - // return new Error('expected object definition') - const newObj: any = {} if (obj) { for (const [key, info] of Object.entries(this._object ?? {})) { diff --git a/src/common/schema/types.ts b/src/common/schema/types.ts index ccd2444..827fecd 100644 --- a/src/common/schema/types.ts +++ b/src/common/schema/types.ts @@ -12,6 +12,7 @@ export interface Type { _default?: T | (() => T) _object?: SchemaDefinitionObject> _union?: Type[] + _integer?: boolean _check: (obj: any) => boolean From d34034e46641fab4633b184aea66c65f836ee01c Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Mon, 26 Aug 2024 13:41:22 +0200 Subject: [PATCH 2/2] 0.24.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 112ea25..8056822 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "zeed", "type": "module", - "version": "0.24.4", + "version": "0.24.5", "description": "🌱 Simple foundation library", "author": { "name": "Dirk Holtwick",