Compare commits

...

3 Commits

Author SHA1 Message Date
ede2283f68
0.0.4 2025-03-25 14:32:02 +01:00
8fa83d2b1f
feat: Add defaultValue to schemas 2025-03-25 14:31:56 +01:00
7d083dde25
fix: compile error 2025-03-25 14:01:18 +01:00
5 changed files with 23 additions and 4 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@dzeio/schema", "name": "@dzeio/schema",
"version": "0.0.3", "version": "0.0.4",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@dzeio/schema", "name": "@dzeio/schema",
"version": "0.0.3", "version": "0.0.4",
"dependencies": { "dependencies": {
"@dzeio/object-util": "^1.8.3", "@dzeio/object-util": "^1.8.3",
"@standard-schema/spec": "^1.0.0", "@standard-schema/spec": "^1.0.0",

View File

@ -1,6 +1,6 @@
{ {
"name": "@dzeio/schema", "name": "@dzeio/schema",
"version": "0.0.3", "version": "0.0.4",
"dependencies": { "dependencies": {
"@dzeio/object-util": "^1.8.3" "@dzeio/object-util": "^1.8.3"
}, },

View File

@ -1,7 +1,9 @@
/* eslint-disable id-length */ /* eslint-disable id-length */
import { objectClean } from '@dzeio/object-util' import { objectClean } from '@dzeio/object-util'
import { StandardSchemaV1 } from '@standard-schema/spec' import { StandardSchemaV1 } from '@standard-schema/spec'
import Schema, { SchemaNullable } from './Schema' import SchemaNullable from 'items/nullable'
import { isNull } from './helpers'
import Schema, { parceable } from './Schema'
import { SchemaJSON, ValidationError, ValidationResult } from './types' import { SchemaJSON, ValidationError, ValidationResult } from './types'
export default abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type> { export default abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type> {
@ -71,6 +73,18 @@ export default abstract class SchemaItem<Type = any> implements StandardSchemaV1
} }
} }
@parceable()
public defaultValue(value: Type, strict = true) {
this.addPreProcess((input) => {
if (strict && isNull(input) || !strict && !input) {
return value
}
return input
})
return this
}
public attrs(...attributes: Array<string>) { public attrs(...attributes: Array<string>) {
this.attributes.concat(attributes) this.attributes.concat(attributes)
return this return this

View File

@ -5,6 +5,10 @@ import SchemaNullable from 'items/nullable'
import SchemaObject from 'items/object' import SchemaObject from 'items/object'
import type SchemaItem from 'SchemaItem' import type SchemaItem from 'SchemaItem'
export function isNull(value: unknown): value is undefined | null {
return typeof value === 'undefined' || value === null
}
export function parseQuery<T extends SchemaItem>(model: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']> { export function parseQuery<T extends SchemaItem>(model: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']> {
const record: Record<string, unknown> = {} const record: Record<string, unknown> = {}
for (const [key, value] of query) { for (const [key, value] of query) {

View File

@ -1,6 +1,7 @@
import SchemaItem from '../SchemaItem' import SchemaItem from '../SchemaItem'
export default class SchemaAny extends SchemaItem { export default class SchemaAny extends SchemaItem {
// @ts-expect-error input while not used is necessary for the `override to work`
public override isOfType(input: unknown): input is any { public override isOfType(input: unknown): input is any {
return true return true
} }