Compare commits
3 Commits
8d6e9d61a1
...
ede2283f68
Author | SHA1 | Date | |
---|---|---|---|
ede2283f68 | |||
8fa83d2b1f | |||
7d083dde25 |
4
package-lock.json
generated
4
package-lock.json
generated
@ -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",
|
||||||
|
@ -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"
|
||||||
},
|
},
|
||||||
|
@ -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
|
||||||
|
@ -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) {
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user