Publish
This commit is contained in:
30
src/system/index.ts
Normal file
30
src/system/index.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
@sinclair/typebox/system
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017-2025 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
---------------------------------------------------------------------------*/
|
||||
|
||||
export * from './policy'
|
||||
export * from './system'
|
||||
77
src/system/policy.ts
Normal file
77
src/system/policy.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
@sinclair/typebox/system
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017-2025 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
---------------------------------------------------------------------------*/
|
||||
|
||||
import { IsObject, IsArray, IsNumber, IsUndefined } from '../value/guard/index'
|
||||
|
||||
export namespace TypeSystemPolicy {
|
||||
// ------------------------------------------------------------------
|
||||
// TypeSystemPolicy: Instancing
|
||||
// ------------------------------------------------------------------
|
||||
/**
|
||||
* Configures the instantiation behavior of TypeBox types. The `default` option assigns raw JavaScript
|
||||
* references for embedded types, which may cause side effects if type properties are explicitly updated
|
||||
* outside the TypeBox type builder. The `clone` option creates copies of any shared types upon creation,
|
||||
* preventing unintended side effects. The `freeze` option applies `Object.freeze()` to the type, making
|
||||
* it fully readonly and immutable. Implementations should use `default` whenever possible, as it is the
|
||||
* fastest way to instantiate types. The default setting is `default`.
|
||||
*/
|
||||
export let InstanceMode: 'default' | 'clone' | 'freeze' = 'default'
|
||||
// ------------------------------------------------------------------
|
||||
// TypeSystemPolicy: Checking
|
||||
// ------------------------------------------------------------------
|
||||
/** Sets whether TypeBox should assert optional properties using the TypeScript `exactOptionalPropertyTypes` assertion policy. The default is `false` */
|
||||
export let ExactOptionalPropertyTypes: boolean = false
|
||||
/** Sets whether arrays should be treated as a kind of objects. The default is `false` */
|
||||
export let AllowArrayObject: boolean = false
|
||||
/** Sets whether `NaN` or `Infinity` should be treated as valid numeric values. The default is `false` */
|
||||
export let AllowNaN: boolean = false
|
||||
/** Sets whether `null` should validate for void types. The default is `false` */
|
||||
export let AllowNullVoid: boolean = false
|
||||
/** Checks this value using the ExactOptionalPropertyTypes policy */
|
||||
export function IsExactOptionalProperty(value: Record<keyof any, unknown>, key: string) {
|
||||
return ExactOptionalPropertyTypes ? key in value : value[key] !== undefined
|
||||
}
|
||||
/** Checks this value using the AllowArrayObjects policy */
|
||||
export function IsObjectLike(value: unknown): value is Record<keyof any, unknown> {
|
||||
const isObject = IsObject(value)
|
||||
return AllowArrayObject ? isObject : isObject && !IsArray(value)
|
||||
}
|
||||
/** Checks this value as a record using the AllowArrayObjects policy */
|
||||
export function IsRecordLike(value: unknown): value is Record<keyof any, unknown> {
|
||||
return IsObjectLike(value) && !(value instanceof Date) && !(value instanceof Uint8Array)
|
||||
}
|
||||
/** Checks this value using the AllowNaN policy */
|
||||
export function IsNumberLike(value: unknown): value is number {
|
||||
return AllowNaN ? IsNumber(value) : Number.isFinite(value)
|
||||
}
|
||||
/** Checks this value using the AllowVoidNull policy */
|
||||
export function IsVoidLike(value: unknown): value is void {
|
||||
const isUndefined = IsUndefined(value)
|
||||
return AllowNullVoid ? isUndefined || value === null : isUndefined
|
||||
}
|
||||
}
|
||||
66
src/system/system.ts
Normal file
66
src/system/system.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
@sinclair/typebox/system
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017-2025 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
---------------------------------------------------------------------------*/
|
||||
|
||||
import { TypeRegistry, FormatRegistry } from '../type/registry/index'
|
||||
import { Unsafe, type TUnsafe } from '../type/unsafe/index'
|
||||
import { Kind } from '../type/symbols/index'
|
||||
import { TypeBoxError } from '../type/error/index'
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Errors
|
||||
// ------------------------------------------------------------------
|
||||
export class TypeSystemDuplicateTypeKind extends TypeBoxError {
|
||||
constructor(kind: string) {
|
||||
super(`Duplicate type kind '${kind}' detected`)
|
||||
}
|
||||
}
|
||||
export class TypeSystemDuplicateFormat extends TypeBoxError {
|
||||
constructor(kind: string) {
|
||||
super(`Duplicate string format '${kind}' detected`)
|
||||
}
|
||||
}
|
||||
// ------------------------------------------------------------------
|
||||
// TypeSystem
|
||||
// ------------------------------------------------------------------
|
||||
export type TypeFactoryFunction<Type, Options = Record<PropertyKey, unknown>> = (options?: Partial<Options>) => TUnsafe<Type>
|
||||
|
||||
/** Creates user defined types and formats and provides overrides for value checking behaviours */
|
||||
export namespace TypeSystem {
|
||||
/** Creates a new type */
|
||||
export function Type<Type, Options = Record<PropertyKey, unknown>>(kind: string, check: (options: Options, value: unknown) => boolean): TypeFactoryFunction<Type, Options> {
|
||||
if (TypeRegistry.Has(kind)) throw new TypeSystemDuplicateTypeKind(kind)
|
||||
TypeRegistry.Set(kind, check)
|
||||
return (options: Partial<Options> = {}) => Unsafe<Type>({ ...options, [Kind]: kind })
|
||||
}
|
||||
/** Creates a new string format */
|
||||
export function Format<F extends string>(format: F, check: (value: string) => boolean): F {
|
||||
if (FormatRegistry.Has(format)) throw new TypeSystemDuplicateFormat(format)
|
||||
FormatRegistry.Set(format, check)
|
||||
return format
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user