Comparing version 0.1.12 to 0.1.13
@@ -83,3 +83,3 @@ import { Primitive } from './primitive'; | ||
static deserialize(type?: string): Type | undefined; | ||
static validate(value: any, type: Type): [boolean, string]; | ||
static validate(value: any, type: Type | string): [boolean, string]; | ||
static type(value: any, options?: TypeOptions): Type; | ||
@@ -86,0 +86,0 @@ private static modifier; |
@@ -180,3 +180,4 @@ "use strict"; | ||
try { | ||
this._validate(value, type); | ||
const _type = typeof type === 'string' ? this.parse(type) : type; | ||
this._validate(value, _type); | ||
return [true, '']; | ||
@@ -183,0 +184,0 @@ } |
{ | ||
"name": "typ3s", | ||
"version": "0.1.12", | ||
"version": "0.1.13", | ||
"description": "Allow define type on json", | ||
@@ -5,0 +5,0 @@ "scripts": { |
114
README.md
@@ -70,2 +70,3 @@ # typ3s | ||
| deserialized | deserialize type | | ||
| validate | validate data with type | | ||
@@ -180,2 +181,15 @@ ## Type method | ||
## Validate method | ||
The validate method allows us to validate data with a type. | ||
```ts | ||
let [isValid, message] = Type.validate(data, '[{name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string}]') | ||
if (!isValid) { | ||
console.log(message) | ||
} else { | ||
console.log('Valid!') | ||
} | ||
``` | ||
## Example | ||
@@ -1219,2 +1233,102 @@ | ||
## Validate example | ||
This example shows how you can validate an object with a type. | ||
```ts | ||
import { Type } from 'typ3s' | ||
const data = [ | ||
{ | ||
name: 'Spain', | ||
region: { name: 'Europe', code: 'EU', description: 'European Union' }, | ||
languages: ['Spanish', 'Catalan', 'Galician', 'Basque'], | ||
phoneCode: 34, | ||
religion: 'Roman Catholicism' | ||
}, | ||
{ | ||
name: 'United Kingdom', | ||
region: { name: 'Europe', code: 'EU', description: 'European Union' }, | ||
languages: ['English'], | ||
phoneCode: 44, | ||
religion: 'Christianity' | ||
} | ||
] | ||
const withoutLanguages = [ | ||
{ | ||
name: 'Italy', | ||
region: { name: 'Europe', code: 'EU', description: 'European Union' }, | ||
phoneCode: 39, | ||
religion: 'Roman Catholicism' | ||
} | ||
] | ||
const withoutRegionCode = [ | ||
{ | ||
name: 'France', | ||
region: { name: 'Europe', description: 'European Union' }, | ||
languages: ['French'], | ||
phoneCode: 33, | ||
religion: 'Roman Catholicism' | ||
} | ||
] | ||
const incorrectPhoneCode = [ | ||
{ | ||
name: 'Argentina', | ||
region: { name: 'South America', code: 'SA', description: 'South America' }, | ||
languages: ['Spanish'], | ||
phoneCode: '54', | ||
religion: 'Roman Catholicism' | ||
} | ||
] | ||
const type = Type.type(data) | ||
const stringified = Type.stringify(type) | ||
let [isValid, message] = Type.validate(data, type) | ||
if (!isValid) { | ||
console.log(message) | ||
} else { | ||
console.log('Valid!') | ||
} | ||
[isValid, message] = Type.validate(withoutLanguages, type) | ||
if (!isValid) { | ||
console.log(message) | ||
} else { | ||
console.log('Valid!') | ||
} | ||
[isValid, message] = Type.validate(withoutRegionCode, stringified) | ||
if (!isValid) { | ||
console.log(message) | ||
} else { | ||
console.log('Valid!') | ||
} | ||
[isValid, message] = Type.validate(incorrectPhoneCode, stringified) | ||
if (!isValid) { | ||
console.log(message) | ||
} else { | ||
console.log('Valid!') | ||
} | ||
``` | ||
**Data Result:** | ||
```sh | ||
Valid! | ||
``` | ||
**WithoutLanguages Result:** | ||
```sh | ||
[{name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string}] item {name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string} property languages [string] value: undefined is not an array | ||
``` | ||
**WithoutRegionCode Result:** | ||
```sh | ||
[{name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string}] item {name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string} property region {name:string,code:string,description:string} property code Value undefined is not a string | ||
``` | ||
**IncorrectPhoneCode Result:** | ||
```sh | ||
[{name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string}] item {name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer,religion:string} property phoneCode Value 54 is not an integer | ||
``` | ||
## Source documentation | ||
@@ -1221,0 +1335,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
122298
1200
1340