Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@travetto/schema

Package Overview
Dependencies
Maintainers
1
Versions
323
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@travetto/schema - npm Package Compare versions

Comparing version 0.0.21 to 0.0.22

2

package.json

@@ -28,3 +28,3 @@ {

},
"version": "0.0.21"
"version": "0.0.22"
}

@@ -40,2 +40,6 @@ import { CommonRegExp, SchemaRegistry, ClassList, ValidatorFn } from '../service';

export const Url = (message?: string) => Match(CommonRegExp.url, message);
export const Precision = (precision: number) => prop({ precision });
export const Integer = () => prop({ precision: 0 });
export const Float = () => prop({ precision: 10 });
export const Currency = () => prop({ precision: 2 });

@@ -42,0 +46,0 @@ export function View(...names: string[]) {

@@ -83,3 +83,7 @@ import { ClassList, FieldConfig, ClassConfig, ViewConfig } from './types';

registerPendingFieldConfig(target: Class, prop: string, type: ClassList) {
registerPendingFieldConfigSpecifierType(target: Class, prop: string, specifier: string) {
return this.registerPendingFieldFacet(target, prop, { declared: { specifier } });
}
registerPendingFieldConfig(target: Class, prop: string, type: ClassList, specifier?: string) {
const isArray = Array.isArray(type);

@@ -90,3 +94,5 @@ const fieldConf: FieldConfig = {

declared: {
array: isArray, type: isArray ? (type as any)[0] : type
array: isArray,
type: isArray ? (type as any)[0] : type,
specifier
}

@@ -93,0 +99,0 @@ };

import { Class } from '@travetto/registry';
import { ValidationError } from '.';
export interface SchemaClass<T = any> {
from?: <Z>(data: any) => Z;
}
export type ClassList = Class | [Class];

@@ -30,5 +26,6 @@

aliases?: string[];
declared: { type: Class<any>, array: boolean };
declared: { type: Class<any>, array: boolean, specifier?: string };
required?: { message?: string };
match?: { re: RegExp, message?: string };
precision?: number;
min?: { n: number | Date, message?: string };

@@ -35,0 +32,0 @@ max?: { n: number | Date, message?: string };

@@ -9,3 +9,3 @@ import { Messages } from './messages';

postal_code: /^\d{5}(?:[-\s]\d{4})?$/
}
};

@@ -12,0 +12,0 @@ // Rebind regexes

@@ -1,2 +0,2 @@

import { SchemaRegistry } from '../service';
import { SchemaRegistry, FieldConfig } from '../service';
import { Class } from '@travetto/registry';

@@ -53,6 +53,13 @@

static coerceType<T>(type: Class<T>, val: any): T {
if (val.constructor !== type) {
const atype = type as Class;
if (atype === Boolean) {
static coerceType<T>(conf: FieldConfig, val: any): T {
const type = conf.declared.type;
if (type === Number) {
if (conf.precision) {
val = +parseFloat(`${val}`).toFixed(conf.precision);
} else {
val = parseInt(`${val}`, 10);
}
} else if (val.constructor !== type) {
if (type === Boolean) {
if (typeof val === 'string') {

@@ -63,8 +70,7 @@ val = val === 'true';

}
} else if (atype === Number) {
val = parseInt(`${val}`, 10);
} else if (atype === String) {
} else if (type === String) {
val = `${val}`;
}
}
return val as T;

@@ -112,3 +118,5 @@ }

if (v !== undefined && v !== null) {
const declared = viewConf.schema[schemaFieldName].declared;
const config = viewConf.schema[schemaFieldName];
const declared = config.declared;
// Ensure its an array

@@ -127,4 +135,4 @@ if (!Array.isArray(v) && declared.array) {

v = declared.array ?
v.map((e: any) => BindUtil.coerceType(declared.type, e)) :
BindUtil.coerceType(declared.type, v);
v.map((e: any) => BindUtil.coerceType(config, e)) :
BindUtil.coerceType(config, v);
}

@@ -131,0 +139,0 @@ }

@@ -76,5 +76,6 @@ import { Class } from '@travetto/registry';

const name = cfg.name.toUpperCase();
const precision = cfg.precision;
if (min !== undefined || max !== undefined) {
return faker.random.number({ min, max });
return faker.random.number({ min, max, precision });
} else {

@@ -81,0 +82,0 @@ for (const [re, fn] of this.NAMES_TO_TYPE.number) {

import {
Field, Url, View, Required, Alias,
BindUtil, Schema, SchemaRegistry, ClassWithSchema
BindUtil, Schema, SchemaRegistry, ClassWithSchema, Float, Integer
} from '../src';

@@ -22,2 +22,3 @@ import { Address } from './address';

@Float()
@Field(Number)

@@ -32,2 +33,5 @@ value: number;

@Integer()
age: number;
@View('test')

@@ -70,2 +74,3 @@ address: Address;

name: 'Test',
age: 19.999978,
address: {

@@ -76,6 +81,8 @@ street1: '1234 Fun',

counts: [
{ area: 'A', value: 20 },
{ area: 'A', value: 20.55555 },
{ area: 'B', value: 30 }
]
});
const a = 30;
assert(person.age === 19);
assert(person.address instanceof Address);

@@ -85,2 +92,3 @@ assert(person.address.street1 === '1234 Fun');

assert(person.counts[0] instanceof Count);
assert(person.counts[0].value === 20.55555);

@@ -87,0 +95,0 @@ const viewPerson = BindUtil.bindSchema(Person, new Person(), {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc