Gathering detailed insights and metrics for @sodaru/yup-to-json-schema
Gathering detailed insights and metrics for @sodaru/yup-to-json-schema
Gathering detailed insights and metrics for @sodaru/yup-to-json-schema
Gathering detailed insights and metrics for @sodaru/yup-to-json-schema
npm install @sodaru/yup-to-json-schema
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
13 Stars
39 Commits
8 Forks
3 Watching
3 Branches
6 Contributors
Updated on 02 Jun 2024
TypeScript (98.28%)
JavaScript (1.72%)
Cumulative downloads
Total Downloads
Last day
2.9%
2,667
Compared to previous day
Last week
-1.6%
14,159
Compared to previous week
Last month
-39.3%
65,054
Compared to previous month
Last year
435%
906,552
Compared to previous year
Yup is a library to validate the JSON input
JSON-Schema is a schema specification for JSON
NOTE : Upgraded to support Yup Version 1 Version Maping
Yup Major Version This Library Major Version Branch in this Library 1 2 main 0 1 yup_v0
This library converts Yup
schema to JSON-Schema
npm i @sodaru/yup-to-json-schema
1import { object, string, array, tuple, date, boolean, number } from 'yup'; 2import { convertSchema } from '@sodaru/yup-to-json-schema'; 3 4const schema = object({ 5 name: string().label('Name').required(), 6 address: tuple([string(), string()]).label('Address'), 7 hobbies: array(string()).min(1).label('Hobbies'), 8 birthdate: date().label('Birthdate'), 9 admin: boolean().label('Admin'), 10 rank: number().integer().positive().label('Rank') 11}); 12 13const jsonSchema = convertSchema(schema); 14 15assert(jsonSchema).toEqual({ 16 type: 'object', 17 required: ['name'] 18 properties: { 19 name: { 20 type: 'string', 21 title: 'Name' 22 }, 23 address: { 24 type: 'array', 25 title: 'Address', 26 minItems: 2, 27 maxItems: 2, 28 items: [ 29 { 30 type: 'string' 31 }, 32 { 33 type: 'string' 34 } 35 ] 36 }, 37 hobbies: { 38 type: 'array', 39 title: 'Hobbies', 40 items: { 41 type: 'string' 42 }, 43 minItems: 1 44 }, 45 birthdate: { 46 type: 'string', 47 title: 'Birthdate', 48 format: 'date-time' 49 }, 50 admin: { 51 type: 'boolean', 52 title: 'Admin' 53 }, 54 rank: { 55 type: 'integer', 56 title: 'Rank', 57 exclusiveMinimum: 0 58 } 59 } 60});
Add additional JSON Schema information to the jsonSchema
property via the meta()
method. This will be shallow merged with the result of the conversion.
1const schema = object({ ... }).meta({ 2 jsonSchema: { 3 $id: '...' 4 description: '...' 5 } 6});
You can also use the extendSchema
to add helpful methods to all of your yup schemas. Use the example
, examples
, description
, and jsonSchema
methods to add common proprties to your JSON Schemas.
1import { 2 object, 3 string, 4 array, 5 tuple, 6 date, 7 boolean, 8 number, 9 addMethod, 10 Schema 11} from 'yup'; 12import { extendSchema, convertSchema } from '@sodaru/yup-to-json-schema'; 13 14extendSchema({ addMethod, Schema }); 15 16const schema = object({ 17 name: string() 18 .label('Name') 19 .description('Your first name') 20 .example('Johnny Cash') 21 .required(), 22 address: tuple([string(), string()]) 23 .label('Address') 24 .description('Your Address') 25 .example(['Nashville', 'TN']), 26 hobbies: array(string()) 27 .label('Hobbies') 28 .min(1) 29 .description('Your hobbies') 30 .example(['Guitar', 'Singing']), 31 birthdate: date() 32 .label('Birthdate') 33 .description('Your birthdate') 34 .example(new Date('February 26, 1932')), 35 admin: boolean() 36 .label('Admin') 37 .description('Use is an admin') 38 .example(true), 39 rank: number() 40 .label('Rank') 41 .description('Your rank') 42 .jsonSchema((jsonSchema) => { 43 return { 44 ...jsonSchema, 45 testProp: true 46 }; 47 }) 48 .example(27) 49 .integer() 50 .positive() 51}); 52 53const jsonSchema = convertSchema(schema); 54 55jsonSchema = { 56 type: 'object', 57 required: ['name'] 58 properties: { 59 name: { 60 type: 'string', 61 title: 'Name', 62 description: 'Your first name', 63 example: 'Johnny Cash' 64 }, 65 address: { 66 type: 'array', 67 title: 'Address', 68 description: 'Your Address', 69 example: ['Nashville', 'TN'], 70 minItems: 2, 71 maxItems: 2, 72 items: [ 73 { 74 type: 'string' 75 }, 76 { 77 type: 'string' 78 } 79 ] 80 }, 81 hobbies: { 82 type: 'array', 83 title: 'Hobbies', 84 items: { 85 type: 'string' 86 }, 87 minItems: 1, 88 description: 'Your hobbies', 89 example: ['Guitar', 'Singing'] 90 }, 91 birthdate: { 92 type: 'string', 93 title: 'Birthdate', 94 format: 'date-time', 95 description: 'Your birthdate', 96 example: '1932-02-26T06:00:00.000Z' 97 }, 98 admin: { 99 type: 'boolean', 100 title: 'Admin', 101 description: 'User is an admin', 102 example: true 103 }, 104 rank: { 105 type: 'integer', 106 title: 'Rank', 107 exclusiveMinimum: 0, 108 description: 'Your rank', 109 testProp: true, 110 example: 27 111 } 112 } 113};
when
and lazy
Use the second argument ResolveOptions
to pass context to allow for better when
and lazy
methods. This argument is passed to the underlying describe(options)
1import { object, number } from "yup"; 2import { convertSchema } from "@sodaru/yup-to-json-schema"; 3 4const schema = object({ 5 rank: number().when(["name"], ([name], schema) => { 6 return name === "Johnny Cash" ? schema.min(100) : schema.min(0); 7 }) 8}); 9 10const schema = object({ 11 rank: lazy(({ name }) => { 12 return name === "Johnny Cash" ? number().min(100) : number().min(0); 13 }) 14}); 15 16const jsonSchema = convertSchema(schema, { 17 value: { name: "Johnny Cash", rank: 100 } 18}); 19 20jsonSchema = { 21 type: "object", 22 properties: { 23 rank: { 24 type: "number", 25 minimum: 100 26 } 27 } 28};
Fork the repo and send the Pull Requests to develop
branch
develop
is merged to the main
branch periodically to make a release
This project is a part of the Open Source Initiative from Sodaru Technologies
Write an email to opensource@sodaru.com for queries on this project
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 5/16 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
10 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn Moreyup
Dead simple Object schema validation
schema-to-yup
Build a Yup schema object to validate models from a domain model schema (JSON or GraphQL)
json-schema-traverse
Traverse JSON Schema passing each schema object to callback
json-schema-yup-transformer
Transforms a draft 7 specification JSON Schema to a Yup Schema