Skip to content

Commit

Permalink
Merge branch 'release/v0.24.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Aug 26, 2024
2 parents 3553e34 + d34034e commit ca3333f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.24.4",
"version": "0.24.5",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down
5 changes: 3 additions & 2 deletions src/common/schema/schema.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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<Status>(),
Expand All @@ -36,6 +36,7 @@ describe('schema', () => {
"type": "boolean",
},
"age": Object {
"_integer": true,
"_optional": true,
"type": "number",
},
Expand Down
15 changes: 8 additions & 7 deletions src/common/schema/schema.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -64,6 +64,13 @@ export function number<T = number>() {
})
}

export function int<T = number>() {
return generic<T>('number', {
_integer: true,
_check: isInteger,
})
}

export function boolean<T = boolean>() {
return generic<T>('boolean', {
_check: isBoolean,
Expand Down Expand Up @@ -94,12 +101,6 @@ export function object<T extends SchemaDefinitionObject>(tobj: T): TypeObject<T>
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 ?? {})) {
Expand Down
1 change: 1 addition & 0 deletions src/common/schema/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface Type<T = unknown> {
_default?: T | (() => T)
_object?: SchemaDefinitionObject<Type<T>>
_union?: Type[]
_integer?: boolean

_check: (obj: any) => boolean

Expand Down

0 comments on commit ca3333f

Please sign in to comment.